选择多个重叠日期范围作为不同列

时间:2017-07-12 19:36:32

标签: sql sql-server

我有一些查询并将其输出与联合组合。 然而,这会占用我的数据库服务器上的CPU。

如果可能,我想删除联合并将它们全部合并为一个语句。

以下是查询:

select [shift],p.wc, (sum(QtyMade) / sum(CREWHrs)) / (max(t.UNITPERHRS) / NULLIF(max(t.CREW),0)) effy ,'YTD' datefield 
from Prod p left join #dtl_temp t on p.PN=t.PARTN  
where p.wc in ('999') and [date]>= DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) - 1, 0) 
group by p.wc,[shift] 

union

select [shift],p.wc, (sum(QtyMade) / sum(CREWHrs)) / (max(t.UNITPERHRS) / NULLIF(max(t.CREW),0)) effy ,'Prior Year' datefield 
from Prod p left join #dtl_temp t on p.PN=t.PARTN  
where p.wc in ('999') and ([date]>= DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) - 1, 0) and [date] < DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0)) 
group by p.wc,[shift] 

union

select [shift],p.wc, (sum(QtyMade) / sum(CREWHrs)) / (max(t.UNITPERHRS) / NULLIF(max(t.CREW),0)) effy ,'QTD' datefield 
from Prod p left join #dtl_temp t on p.PN=t.PARTN  
where p.wc in ('999') and ([date] >= '2017-07-01' and [date] < '2017-07-13') 
group by p.wc,[shift] 

union

select [shift],p.wc, (sum(QtyMade) / sum(CREWHrs)) / (max(t.UNITPERHRS) / NULLIF(max(t.CREW),0)) effy ,'MTD' datefield 
from Prod p left join #dtl_temp t on p.PN=t.PARTN  
where p.wc in ('999') and ([date] >= '2017-07-01' and [date] < '2017-07-13') 
group by p.wc,[shift] 

union

select [shift],p.wc, (sum(QtyMade) / sum(CREWHrs)) / (max(t.UNITPERHRS) / NULLIF(max(t.CREW),0)) effy ,'WTD' datefield 
from Prod p left join #dtl_temp t on p.PN=t.PARTN  
where p.wc in ('999') and ([date] >= '2017-07-10' and [date] < '2017-07-13') 
group by p.wc,[shift] 

union

select [shift],p.wc, (sum(QtyMade) / sum(CREWHrs)) / (max(t.UNITPERHRS) / NULLIF(max(t.CREW),0)) effy ,'Prior Quarter' datefield 
from Prod p left join #dtl_temp t on p.PN=t.PARTN  
where p.wc in ('999') and ([date] >= '2017-04-01' and [date] < '2017-07-01') 
group by p.wc,[shift] 

输出是:

shift   wc  effy                datefield
1       999 0.951558640486598   YTD
2       999 0.706728843730067   YTD
3       999 0.831153497545209   YTD
1       999 0.732836888916855   Prior Year
2       999 0.548579728223933   Prior Year
3       999 0.58957623455441    Prior Year
1       999 0.617329386562882   QTD
2       999 0.826112085298484   QTD
3       999 1.0719875569761     QTD
1       999 0.617329386562882   MTD
2       999 0.826112085298484   MTD
3       999 1.0719875569761     MTD
1       999 0.617329386562882   WTD
2       999 0.832089035926471   WTD
3       999 1.0719875569761     WTD
1       999 1.17254367493512    Prior Quarter
2       999 0.939448907844456   Prior Quarter
3       999 1.20640432761946    Prior Quarter

我有办法以某种方式将所有这些日期范围合并到CASE语句中吗?或者我是否需要创建一个临时表,其中包含日期类别(WTD,MTD,QTD等)和#34;来自&#34;和&#34;到&#34;?

如果没有工会,我不确定如何才能实现这一结果。

您在SELECT中看到的日期来自一个计算它们并创建sql查询的脚本。

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您将通过以下查询获得结果:我认为您正在通过分组或汇总或多维数据集来寻找组。检查哪一个符合您的要求

select [shift],p.wc, (sum(QtyMade) / sum(CREWHrs)) / (max(t.UNITPERHRS) / NULLIF(max(t.CREW),0)) effy ,'Prior Year' datefield 
from Prod p left join #dtl_temp t on p.PN=t.PARTN  
where p.wc in ('999') 
group by grouping sets ([shift], p.wc,datepart(yyyy,[date]), datepart(qq, [date]), datepart(mm,[date]))

答案 1 :(得分:0)

是的,您可以为每个日期范围创建单独的列,并且只有符合日期范围条件的SUM行(未经测试,语法可能不完全正确)...

SUM( CASE WHEN ([date] >= '2017-07-10' and [date] < '2017-07-13') THEN QtyMade ELSE 0 END )

在较新版本的SQL Server上,您还可以使用IIF()

另一方面,UNION ALLUNION便宜,因为它不会消除重复的行。