确定每个 ID 存在 2 个列值:SQL

identify 2 column values exist per ID : SQL

这是我的示例数据

ID    desc           date
------------------------------
1     started       10-01-2020
1     restarted     10-01-2020
1     closed        10-01-2020
1     clear         10-01-2020
2     started       11-02-2020
2     closed        12-01-2020
2     cleared       12-01-2020
3     started       12-01-2020
3     restarted     12-01-2020
3     restarted     12-01-2020
3     clear         12-01-2020

现在我的预期输出是计算有多少不同的 ID 在其 desc 列中同时“启动”和“重新启动”至少一次。

预期输出:

month       distinctIDcount   count_started   count_restarted
Jan 2020          2                2                3

这是我目前的情况:

我能够分别找到那些已经启动的和那些已经重新启动的,但我只需要找到两者都存在的那些。

select 
    Format(date, 'MMMM-yyyy') as Month, 
    sum(count_started) as count_started, 
    sum(count_restarted) as count_restarted
from   
    (select 
         date,
         case when description = 'started' then 1 else 0 end as count_started,
         case when description = 'restarted' then 1 else 0 end as count_restarted,
     from 
         (select * 
          from #temp2 
          where date > '2020-01-01 00:00:00') a
    ) b
group by 
    Format(date, 'MMMM-yyyy')

我们可以尝试先按 ID 和月份进行汇总,然后再按月份进行第二次汇总:

WITH cte AS (
    SELECT ID, FORMAT(date, 'MMMM-yyyy') AS Month,
               COUNT(CASE WHEN desc = 'started' THEN 1 END) AS count_started,
               COUNT(CASE WHEN desc = 'restarted' THEN 1 END) AS count_restarted,
               CASE WHEN COUNT(CASE WHEN desc = 'started' THEN 1 END) > 0 AND
                         COUNT(CASE WHEN desc = 'restarted' THEN 1 END) > 0
                    THEN 1 ELSE 0 END AS count_both
    FROM yourTable
    GROUP BY ID, FORMAT(date, 'MMMM-yyyy')
)

SELECT
    Month,
    SUM(count_both) AS distinctIDcount,
    SUM(count_started) AS count_started,
    SUM(count_restarted) AS count_restarted
FROM cte
GROUP BY
    Month;