sql - 计算 SQL 中 case 语句的行数

标签 sql count 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;

任何帮助将不胜感激

最佳答案

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;

关于sql - 计算 SQL 中 case 语句的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5718220/

相关文章:

sql - 列下归零

mysql - 使用 like 和 not like MySql 选择值时出错

mongodb - 分组数据的聚合计数

sql - Sequelize - 在不同条件下对相同关联进行两次计数

java - 切换选项和默认值

php - 反转字符串中所有字母的大小写(大写到小写和小写到大写)

php - 如何通过使用 ('disabled' 而不是 'class' 在 php 中使用 removeAttr 'id' )?

mysql - 3表SQL查询

codeigniter - COUNT/GROUP BY 与事件记录?

hibernate - Hibernate 3.0 中的条件映射?