我有一个表中的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