一些SQL刷牙......我想爲客戶創造的銷售額每年(2005- 2008年)彙總列表,但只會顯示客戶銷售在以前(2005- 2007年)還沒有銷售在2008年銷售總額:與以往的顯示客戶,但沒有目前的銷售
我已經建立了兩個查詢...不知道哪個更有效,但無論哪種方式,我似乎無法弄清楚如何正確地執行where子句。
任何幫助將不勝感激!
查詢1:
select r.ResellerName,
(select sum(rs.SalesAmount) from FactResellerSales rs
where rs.ResellerKey = r.ResellerKey
and rs.OrderDateKey >=20050101 and rs.OrderDateKey <20060101) '2005',
(select sum(rs.SalesAmount) from FactResellerSales rs
where rs.ResellerKey = r.ResellerKey
and rs.OrderDateKey >=20060101 and rs.OrderDateKey <20070101) '2006',
(select sum(rs.SalesAmount) from FactResellerSales rs
where rs.ResellerKey = r.ResellerKey
and rs.OrderDateKey >=20070101 and rs.OrderDateKey <20080101) '2007',
(select sum(rs.SalesAmount) from FactResellerSales rs
where rs.ResellerKey = r.ResellerKey
and rs.OrderDateKey >=20080101 and rs.OrderDateKey <20090101) '2008'
From DimReseller r
order by r.ResellerName ASC
查詢2:
Select r.ResellerName,
SUM(case when OrderDateKey>=20050101 and OrderDateKey<20060101 then rs.SalesAmount else 0 end) '2005',
SUM(case when OrderDateKey>=20060101 and OrderDateKey<20070101 then rs.SalesAmount else 0 end) '2006',
SUM(case when OrderDateKey>=20070101 and OrderDateKey<20080101 then rs.SalesAmount else 0 end) '2007',
SUM(case when OrderDateKey>=20080101 and OrderDateKey<20090101 then rs.SalesAmount else 0 end) '2008'
from DimReseller r
inner join FactResellerSales rs on rs.ResellerKey = r.ResellerKey
Group by r.ResellerName
Order by ResellerName ASC
'2008'= 0將始終爲假。沒有記錄將返回此查詢。你正在檢查字符串'2008'是否等於0.如果你想這樣做刪除單引號。 – Jeffrey
年,我試過2008 = 0和'2008'= 0無效... – user3221111
傑弗裏,感謝您的幫助,不確定您的查詢是否正在做我想做的事(我是逆向工程)...但最終想要顯示的客戶,2008年沒有銷售,但銷售前幾年...... – user3221111