我不能使用公用表表達式:如何按多個聯合聲明的結果排序?
WITH cte
AS (SELECT [StationID],
[LastDistribution]
FROM [DB1].[dbo].[ProcessingStations]
UNION ALL
SELECT [StationID],
[LastDistribution]
FROM [DB2].[dbo].[ProcessingStations]
UNION ALL
SELECT [StationID],
[LastDistribution]
FROM [DB3].[dbo].[ProcessingStations]
UNION ALL
SELECT [StationID],
[LastDistribution]
FROM [DB4].[dbo].[ProcessingStations]
UNION ALL
SELECT [StationID],
[LastDistribution]
FROM [DB5].[dbo].[ProcessingStations]
ORDER BY [StationID]
UNION ALL
SELECT [StationID],
[LastDistribution]
FROM [DB6].[dbo].[ProcessingStations]
UNION ALL
SELECT [StationID],
[LastDistribution]
FROM [DB7].[dbo].[ProcessingStations]
UNION ALL
SELECT [StationID],
[LastDistribution]
FROM [DB8].[dbo].[ProcessingStations])
SELECT *
FROM cte
ORDER BY StationID
我怎麼會去這樣做呢?
如何訂購記錄? – 2013-04-10 13:47:29
從最低站號到聯盟中所有表中最高的站點。 – 2013-04-10 13:48:20
把'ORDER BY'放在最後。例如:'SELECT ... UNION SELECT ... UNION SELECT ... ORDER BY ...' – valverij 2013-04-10 13:48:56