Where 子句中的 SQL Case 语句
我有一个带有accounting_period的表,其形式为2011年1月的201101,2011年2月的201102,等等。
我正在尝试对本季度的一列 (eff_cc) 进行求和。也就是说,我想获得一月、二月和二月的数据总和。 Mar 2011 表示第一季度的日期等。
因此,我在 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
I have a table with accounting_period in the form of 201101 for Jan 2011, 201102 for Feb 2011, etc.
I am trying to sum a column (eff_cc) for the quarter. That is, I want to get the sum the data for Jan, Feb & Mar 2011 for a date in the 1st quarter, etc.
So, I used a Case statement in the where clause. Basically I said (in the where clause) that:
- if the current month is 1,4,7 or 10, then get data from that month;
- if the current month is 2,5,8 or 11, then get the data from the curent month & the previous month; and
- if the current month is 3,6,9, or 12 then get data from the current and the previous two months
Doesn't want to work. The code is below.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以为此使用 CTE:(
我还假设对所有条目使用事务日期而不是 getdate())
插入行几次后的结果:
You could use CTE for this:
(I also made an assumption of using a transaction date instead of getdate() for all entries)
Results, after inserting the rows a few times:
感谢你们所有人的及时和有用的回复(没有居高临下),
我根据你们的意见设计了一个解决方案。本质上,我创建了一个包含相关数据的子查询和一个新列 (Qtr)。此列评估accounting_period,并为每行分配1、2、3或4。
然后,我围绕这个子查询包装了另一个选择,用一个 where 子句将“Qtr”与当前季度(来自 getDate)进行比较
,也许这效率低下,但我每周只运行一次,因此性能不是一个大问题。
再次感谢大家。
Thanks to all of you for the prompt and useful responses (without being condescending)
I kinda devised a solution based on your input. In essence, I created a subquery, with the relevant data, and a new column (Qtr). This column evaluates the accounting_period, and assigns 1,2,3 or 4 to each row.
Then, I wrapped another select around this subquery, with a where clause comparing the 'Qtr' to the current quarter (from getDate)
Maybe this is inefficient, but I run this only once a week, so performance is not a big issue.
Thanks again to all.
这不是编写 CASE 语句的正确方法,因为它返回一个 BOOLEAN,这在 SQL Server 中不能单独存在。只需将它们分成 3 个 OR 子句
That is not the right way to write a CASE statement, as it is, it returns a BOOLEAN, which in SQL Server cannot stand alone. Just split them into 3 OR clauses
试试这个(我假设它是 SQL Server):
例如:
Try this(I assume its SQL Server):
e.g: