计算行多列

count rows multiple columns

我想根据多列和列中的特定值对行进行计数。请查看附件table.

计算价值:

ID Date Action = 'C'

我试过了

COUNT(*) OVER (PARTITION BY ID, Date, Action = 'C') AS Count,但是没有用。你们有谁知道如何计算这个字段吗?

谢谢。

这是你想做的吗?

select 
     ID, 
     Date, 
     Action, 
     case Action 
         when 'C' then COUNT(*) OVER (PARTITION BY ID, Date)
         else null
     end as count
from your_table

我想你想要:

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

我想你想要:

select t.*,
       (select count(*)
        from table t1
        where t1.date = t.date and t1.id = t.id and t1.action = 'C'
       )
from table t;