我不能使用公用表表达式:
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
我将如何做到这一点?
答案 0 :(得分:5)
只需将ORDER BY
放在SELECT ... FROM ... UNION ALL
语句链的末尾:
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]
ORDER BY StationID
以下是我在SSMS中做的一个简单示例:
DECLARE @a table (x int)
DECLARE @b table (x int)
DECLARE @c table (x int)
insert into @a values (5)
insert into @a values (4)
insert into @a values (3)
insert into @b values (0)
insert into @b values (1)
insert into @b values (2)
insert into @c values (0)
insert into @c values (1)
insert into @c values (2)
select * from @a
union all
select * from @b
union all
select * from @c
order by x
这是输出:
x
-----
0
0
1
1
2
2
3
4
5
正如您所看到的,即使SELECT * FROM @a
排在第一位,它仍将最后一个放在结果集中
答案 1 :(得分:2)
为什么不能使用CTE
?无论如何,您仍然可以使用派生表执行相同的操作:
SELECT *
FROM ( 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]
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]) A
ORDER BY StationID
答案 2 :(得分:0)
请参阅fiddle,只需将ORDER BY
放在最后一组即可“联合”。它可以在没有CTE开销的情况下实现相同的结果。
订单将影响整个统一集。
SELECT
[StationID],
[LastDistribution]
FROM
[dbo].[ProcessingStations]
UNION ALL
SELECT
[StationID],
[LastDistribution]
FROM
[dbo].[ProcessingStations]
UNION ALL
SELECT
[StationID],
[LastDistribution]
FROM
[dbo].[ProcessingStations]
ORDER BY
[StationID]