计算 SQL 中 case 语句的行数

发布于 2024-11-02 20:51:37 字数 478 浏览 1 评论 0原文

我正在尝试在一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

调妓 2024-11-09 20:51:37
select BU,
       sum(CASE when a.date_added = trunc(sysdate) 
                then 1 
                else 0 
            end ) as TodayQuotes,
       sum(CASE when a.date_added > last_day(add_months(sysdate,-1)) 
                then 1 
                else 0 
            end) as MTDQuotesValue,
       sum(case when to_number(to_char(a.date_added,'yyyy'))='2011' 
                then 1 
                else 0 
            end) as YTDRegularValue
  from articles
 group by BU;
select BU,
       sum(CASE when a.date_added = trunc(sysdate) 
                then 1 
                else 0 
            end ) as TodayQuotes,
       sum(CASE when a.date_added > last_day(add_months(sysdate,-1)) 
                then 1 
                else 0 
            end) as MTDQuotesValue,
       sum(case when to_number(to_char(a.date_added,'yyyy'))='2011' 
                then 1 
                else 0 
            end) as YTDRegularValue
  from articles
 group by BU;
花伊自在美 2024-11-09 20:51:37

CASE 表达式中删除 "else '0'"(存在隐式 ELSE NULL

COUNT 计数所有 NOT NULL 值且 '0'NOT NULL,因此会影响 COUNT

Remove the "else '0'" from your CASE expressions (there is an implicit ELSE NULL)

COUNT counts all NOT NULL values and '0' is NOT NULL and so contributes to the COUNT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文