计算 SQL 中 case 语句的行数
我正在尝试在一个 sql 语句查询中获取今天、本月至今、今年迄今为止的行数(输入)。但我不确定为什么它给了我所有三个相同的值。 这是我的sql语句。
select BU,
count(CASE when a.date_added = trunc(sysdate) then (part) else '0' end)
as TodayQuotes,
count(CASE when a.date_added > last_day(add_months(sysdate,-1)) then (part) else '0' end)
as MTDQuotesValue,
COUNT(case when to_number(to_char(a.date_added,'yyyy'))='2011' then (part) else '0' end)
as YTDRegularValue
from articles
group by BU;
任何帮助将不胜感激
I am trying to get the lines count(entered) for Today, Month to date, year to date in one sql statement query. but I am not sure why it is giving me the same values for all the the three.
here is my sql statement.
select BU,
count(CASE when a.date_added = trunc(sysdate) then (part) else '0' end)
as TodayQuotes,
count(CASE when a.date_added > last_day(add_months(sysdate,-1)) then (part) else '0' end)
as MTDQuotesValue,
COUNT(case when to_number(to_char(a.date_added,'yyyy'))='2011' then (part) else '0' end)
as YTDRegularValue
from articles
group by BU;
any help will be appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从
CASE
表达式中删除"else '0'"
(存在隐式ELSE NULL
)COUNT
计数所有NOT NULL
值且'0'
为NOT NULL
,因此会影响COUNT
。Remove the
"else '0'"
from yourCASE
expressions (there is an implicitELSE NULL
)COUNT
counts allNOT NULL
values and'0'
isNOT NULL
and so contributes to theCOUNT
.