我有一個表中的201101爲2011年1月,爲201102 2011年2月的形式accounting_period等
我試圖總結本季度的一列(eff_cc)。也就是說,我想要得到1月份的數據,2011年3月的第一季度的日期等數據。SQL Case語句的where子句
因此,我在where子句中使用了一個Case語句。基本上我說(在where子句中):
- 如果當前月份是1,4,7或10,那麼從那個月份得到數據;
- 如果當前月份爲2,5,8或11,則從前月的月份&獲得數據;和
- 如果當前月份爲3,6,9或12然後從當前和前兩個月
不希望工作中獲取數據。代碼如下。
select phase_code, accounting_period, sum(eff_cc) as BD_Eff_QTD,
from prj_detail
where
case month(getdate()) % 3
when 1 then -- current month is 1,4,7,10
accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())),2)
when 2 then -- current month is 2, 5, 8, 11
(accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())),2) or
accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())-1),2))
when 3 then -- current month is 3, 6, 9, 12
(accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())),2) or
accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())-1),2) or
accounting_period = right(Year(getDate()),4) + Right('0' + rtrim(month(getDate())-2),2))
end
group by phase_code, accounting_period
**什麼**數據庫,哪個版本?? – 2011-02-02 22:03:42
什麼不行?你是否收到錯誤或無效的數據? – 2011-02-02 22:05:45
我不明白,爲什麼你要與`GETDATE()`比較,輸出取決於今天的日期?......你不是隻想爲每個日期的排序器加總和(eff_cc)存儲在你的桌子上? – Lamak 2011-02-02 22:08:44