我如何结合所有2个查询,但是在我将所有查询合并之前还要对每个查询进行排序? 我希望在结果按总和([docextprice])desc
订购后的每一年获得前20名的结果这是我现在拥有的一个例子:
SELECT top 20 gm.[partnum]
,p.PartDescription
,sum([docextprice])[docextprice]
,year([invoicedate])year
,'Eco Surfaces' product
FROM dbo.gm gm
JOIN dbo.Part p on gm.partnum = p.PartNum
WHERE gm.company = 'EII' and [invoicedate] BETWEEN '20100809' and '20101231'
and p.ClassID in ('mpe')
GROUP BY year([invoicedate]),gm.[partnum],p.PartDescription
ORDER BY year([invoicedate]),sum([docextprice]) desc
UNION ALL
SELECT top 20 gm.[partnum]
,p.PartDescription
--,gm.[prodcode]
--,p.ClassID
,sum([docextprice])[docextprice]
,year([invoicedate])year
,'Eco Surfaces' product
FROM dbo.gm gm
JOIN dbo.Part p on gm.partnum = p.PartNum
WHERE gm.company = 'EII' and [invoicedate] BETWEEN '20110101' and '20111231'
and p.ClassID in ('mpe')
GROUP BY year([invoicedate]),gm.[partnum],p.PartDescription
ORDER BY year([invoicedate]),sum([docextprice]) desc
显然,这个查询不会赢,因为您无法在联接的两边订购。
答案 0 :(得分:5)
您可以通过使用公用表表达式来保存结果,然后将它们合并在一起:
WITH cte1
AS ( SELECT TOP 20
gm.[partnum] ,
p.PartDescription ,
SUM([docextprice]) [docextprice] ,
YEAR([invoicedate]) year ,
'Eco Surfaces' product
FROM dbo.gm gm
JOIN dbo.Part p ON gm.partnum = p.PartNum
WHERE gm.company = 'EII'
AND [invoicedate] BETWEEN '20100809' AND '20101231'
AND p.ClassID IN ( 'mpe' )
GROUP BY YEAR([invoicedate]) ,
gm.[partnum] ,
p.PartDescription
ORDER BY YEAR([invoicedate]) ,
SUM([docextprice]) DESC
),
cte2
AS ( SELECT TOP 20
gm.[partnum] ,
p.PartDescription
--,gm.[prodcode]
--,p.ClassID
,
SUM([docextprice]) [docextprice] ,
YEAR([invoicedate]) year ,
'Eco Surfaces' product
FROM dbo.gm gm
JOIN dbo.Part p ON gm.partnum = p.PartNum
WHERE gm.company = 'EII'
AND [invoicedate] BETWEEN '20110101' AND '20111231'
AND p.ClassID IN ( 'mpe' )
GROUP BY YEAR([invoicedate]) ,
gm.[partnum] ,
p.PartDescription
ORDER BY YEAR([invoicedate]) ,
SUM([docextprice]) DESC
)
SELECT *
FROM cte1
UNION ALL
SELECT *
FROM cte2
答案 1 :(得分:2)
您可以在没有UNION
的情况下执行此操作:
;WITH CTE1 AS
(
SELECT gm.[partnum]
,p.PartDescription
,sum([docextprice])[docextprice]
,year([invoicedate]) [year]
,'Eco Surfaces' product
FROM dbo.gm gm
JOIN dbo.Part p on gm.partnum = p.PartNum
WHERE gm.company = 'EII'
and [invoicedate] BETWEEN '20100809' and '20111231'
and p.ClassID in ('mpe')
GROUP BY year([invoicedate]),gm.[partnum],p.PartDescription
), CTE2 AS
(
SELECT *,
RN=ROW_NUMBER() OVER(PARTITION BY [year] ORDER BY [docextprice] DESC)
FROM CTE1
)
SELECT *
FROM CTE2
WHERE RN <= 20
答案 2 :(得分:-1)
您应该可以在原始脚本的第一个查询中删除订单。您只能批量订购一个订单。
SELECT top 20 gm.[partnum]
,p.PartDescription
,sum([docextprice])[docextprice]
,year([invoicedate])year
,'Eco Surfaces' product
FROM dbo.gm gm
JOIN dbo.Part p on gm.partnum = p.PartNum
WHERE gm.company = 'EII' and [invoicedate] BETWEEN '20100809' and '20101231'
and p.ClassID in ('mpe')
GROUP BY year([invoicedate]),gm.[partnum],p.PartDescription
UNION ALL
SELECT top 20 gm.[partnum]
,p.PartDescription
--,gm.[prodcode]
--,p.ClassID
,sum([docextprice])[docextprice]
,year([invoicedate])year
,'Eco Surfaces' product
FROM dbo.gm gm
JOIN dbo.Part p on gm.partnum = p.PartNum
WHERE gm.company = 'EII' and [invoicedate] BETWEEN '20110101' and '20111231'
and p.ClassID in ('mpe')
GROUP BY year([invoicedate]),gm.[partnum],p.PartDescription
ORDER BY year([invoicedate]),sum([docextprice]) desc