我想基于多列并以一列中的特定值对行进行计数。请检查所附表格。
计算以下值:
ID Date Action = 'C'
我尝试了
COUNT(*) OVER (PARTITION BY ID, Date, Action = 'C') AS Count
,但是没有用。你们当中有人知道如何计算此字段吗?
谢谢。
答案 0 :(得分:0)
这是您想做什么?
select
ID,
Date,
Action,
case Action
when 'C' then COUNT(*) OVER (PARTITION BY ID, Date)
else null
end as count
from your_table
答案 1 :(得分:0)
我想你想要
SELECT . . .,
(CASE WHEN Action = 'C'
THEN COUNT(*) OVER (PARTITION BY ID, Date, Action)
END) as c_count
这会将计数放在带有Action = 'C'
的行中。如果要在所有行中计数,则:
SELECT . . .,
COUNT(CASE WHEN Action = 'C' THEN 1 ELSE 0 END) OVER (PARTITION BY ID, Date) as c_count
答案 2 :(得分:0)
我想你想要>
select t.*,
(select count(*)
from table t1
where t1.date = t.date and t1.id = t.id and t1.action = 'C'
)
from table t;