如何处理 T-SQL 中具有 "Union All" 但第二个 "Select" 始终出现在底部的查询?
How do I Process a Query in T-SQL that has a "Union All" but get the Second "Select" to always appear at the bottom?
如何处理 T-SQL 中具有“联合所有”但第二个“Select”始终出现在底部的查询?我有以下 SQL...
select Policy.[Effective Date], Policy.[Policy #], CLIENT.[NAME], Policy.[Premium]
From Policy, Client
Where Policy.[Effective Date] > '2000-11-11' AND Policy.[Policy Type] <> 'Quote' AND Client.[CLIENTID] = Policy.[CLIENTID]
union all
select null, null, 'Totals', sum(Policy.[Premium])
From Policy, Client
Where Policy.[Effective Date] > '2000-11-11' AND Policy.[Policy Type] <> 'Quote' AND Client.
CLIENTID = Policy.CLIENTID
Order By CLIENT.NAME
但结果看起来像...
你需要额外添加一个固定的排序列。这通常是非常有效的,因为它通常使用Merge Concatenation
,as explained by Paul White。
我建议您不要使用古老且已弃用的 ,
连接语法。我还建议您不要使用需要引用的列名。
select Policy.[Effective Date], Policy.[Policy #], CLIENT.[NAME], Policy.[Premium], 1 AS ordering
From Policy
JOIN Client ON Client.[CLIENTID] = Policy.[CLIENTID]
Where Policy.[Effective Date] > '2000-11-11' AND Policy.[Policy Type] <> 'Quote'
union all
select null, null, 'Totals', sum(Policy.[Premium]), 2
From Policy
JOIN Client ON Client.CLIENTID = Policy.CLIENTID
Where Policy.[Effective Date] > '2000-11-11' AND Policy.[Policy Type] <> 'Quote'
Order By Ordering, CLIENT.NAME
如果你不想ordering
出现在结果中那么你需要子查询:
select [Effective Date], [Policy #], [NAME], [Premium]
FROM (
select Policy.[Effective Date], Policy.[Policy #], CLIENT.[NAME], Policy.[Premium], 1 AS ordering
From Policy
JOIN Client ON Client.[CLIENTID] = Policy.[CLIENTID]
Where Policy.[Effective Date] > '2000-11-11' AND Policy.[Policy Type] <> 'Quote'
union all
select null, null, 'Totals', sum(Policy.[Premium]), 2
From Policy
JOIN Client ON Client.CLIENTID = Policy.CLIENTID
Where Policy.[Effective Date] > '2000-11-11' AND Policy.[Policy Type] <> 'Quote'
)
Order By Ordering, CLIENT.NAME
分组集
我注意到您在这里所做的只是第一个结果的汇总。所以你实际上可以使用 GROUPING SETS
syntax 来获得你想要的结果而无需额外的查询。它会更有效率:
select Policy.[Effective Date], Policy.[Policy #], ISNULL(CLIENT.[NAME], 'Totals') AS Name, SUM(Policy.[Premium])
From Policy
JOIN Client ON Client.[CLIENTID] = Policy.[CLIENTID]
Where Policy.[Effective Date] > '2000-11-11' AND Policy.[Policy Type] <> 'Quote'
GROUP BY GROUPING SETS (
(Policy.[Effective Date], Policy.[Policy #], CLIENT.[NAME], Policy.[Premium]),
()
)
ORDER BY
GROUPING_ID(Policy.[Effective Date], Policy.[Policy #], CLIENT.[NAME], Policy.[Premium]),
CLIENT.[NAME]
function GROUPING_ID
returns 该行中聚合的每一列的 1 位,因此对于我们的总数,我们得到位 1111
排序在 0000
之后其余结果。
如何处理 T-SQL 中具有“联合所有”但第二个“Select”始终出现在底部的查询?我有以下 SQL...
select Policy.[Effective Date], Policy.[Policy #], CLIENT.[NAME], Policy.[Premium]
From Policy, Client
Where Policy.[Effective Date] > '2000-11-11' AND Policy.[Policy Type] <> 'Quote' AND Client.[CLIENTID] = Policy.[CLIENTID]
union all
select null, null, 'Totals', sum(Policy.[Premium])
From Policy, Client
Where Policy.[Effective Date] > '2000-11-11' AND Policy.[Policy Type] <> 'Quote' AND Client.
CLIENTID = Policy.CLIENTID
Order By CLIENT.NAME
但结果看起来像...
你需要额外添加一个固定的排序列。这通常是非常有效的,因为它通常使用Merge Concatenation
,as explained by Paul White。
我建议您不要使用古老且已弃用的 ,
连接语法。我还建议您不要使用需要引用的列名。
select Policy.[Effective Date], Policy.[Policy #], CLIENT.[NAME], Policy.[Premium], 1 AS ordering
From Policy
JOIN Client ON Client.[CLIENTID] = Policy.[CLIENTID]
Where Policy.[Effective Date] > '2000-11-11' AND Policy.[Policy Type] <> 'Quote'
union all
select null, null, 'Totals', sum(Policy.[Premium]), 2
From Policy
JOIN Client ON Client.CLIENTID = Policy.CLIENTID
Where Policy.[Effective Date] > '2000-11-11' AND Policy.[Policy Type] <> 'Quote'
Order By Ordering, CLIENT.NAME
如果你不想ordering
出现在结果中那么你需要子查询:
select [Effective Date], [Policy #], [NAME], [Premium]
FROM (
select Policy.[Effective Date], Policy.[Policy #], CLIENT.[NAME], Policy.[Premium], 1 AS ordering
From Policy
JOIN Client ON Client.[CLIENTID] = Policy.[CLIENTID]
Where Policy.[Effective Date] > '2000-11-11' AND Policy.[Policy Type] <> 'Quote'
union all
select null, null, 'Totals', sum(Policy.[Premium]), 2
From Policy
JOIN Client ON Client.CLIENTID = Policy.CLIENTID
Where Policy.[Effective Date] > '2000-11-11' AND Policy.[Policy Type] <> 'Quote'
)
Order By Ordering, CLIENT.NAME
分组集
我注意到您在这里所做的只是第一个结果的汇总。所以你实际上可以使用 GROUPING SETS
syntax 来获得你想要的结果而无需额外的查询。它会更有效率:
select Policy.[Effective Date], Policy.[Policy #], ISNULL(CLIENT.[NAME], 'Totals') AS Name, SUM(Policy.[Premium])
From Policy
JOIN Client ON Client.[CLIENTID] = Policy.[CLIENTID]
Where Policy.[Effective Date] > '2000-11-11' AND Policy.[Policy Type] <> 'Quote'
GROUP BY GROUPING SETS (
(Policy.[Effective Date], Policy.[Policy #], CLIENT.[NAME], Policy.[Premium]),
()
)
ORDER BY
GROUPING_ID(Policy.[Effective Date], Policy.[Policy #], CLIENT.[NAME], Policy.[Premium]),
CLIENT.[NAME]
function GROUPING_ID
returns 该行中聚合的每一列的 1 位,因此对于我们的总数,我们得到位 1111
排序在 0000
之后其余结果。