我试图在一个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;
任何帮助将不胜感激
答案 0 :(得分:2)
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;
答案 1 :(得分:1)
从"else '0'"
表达式中删除CASE
(隐含ELSE NULL
}
COUNT
计算所有NOT NULL
个值,'0'
为NOT NULL
,因此对COUNT
有所贡献。