2015-10-06 54 views
0

我有兩個單獨的查詢,下面哪些運行正確。現在,我創建了一個計算列以提供YM工作日的計數,並希望將這通過query1(加入將是query1.Period = query2.Yms) 請參閱下面的查詢和輸出。通過在另一個查詢中新創建的計算列

SELECT  Client, ClientGroup, Type, Value, Period, PeriodName, PeriodNumber, ClientName 
FROM   metrics.dbo.vw_KPI_001_Invoice 



select YMs,sum(case when IsWorkDay = 'X' then 1 else 0 end) from IESAONLINE.Dbo.DS_Dates 
where Year > '2013' 
group by YMs 

Query 1 
Client ClientGroup Type Value Period PeriodName  PeriodNumber ClientName 
0LG0 KarroFoods Stock 5691.68 201506 Week 06 2015 35 Karro Foods Scunthorpe 

Query 2 
YMs (No column name) 
201401 23 

回答

0

請問以下工作:

SELECT Client, ClientGroup, Type, Value, Period, PeriodName, PeriodNumber, ClientName, cnt 
FROM   metrics.dbo.vw_KPI_001_Invoice q1 
INNER JOIN (select YMs,sum(case when IsWorkDay = 'X' then 1 else 0 end) as cnt from IESAONLINE.Dbo.DS_Dates 
where Year > '2013' 
group by YMs) q2 ON q1.Period = q2.YMs 

如果值並不總是可用的,那麼你可能會考慮改變INNER JOIN到外連接。

相關問題