【问题标题】:Create separate columns for counts of occurrences of values in column为列中值的出现次数创建单独的列
【发布时间】:2022-07-19 20:34:26
【问题描述】:

我有一个类似这样的数据集:

ID Status
A Enabled
A Disabled
A Enabled
B Enabled
B Disabled

我想调整数据结构,使其看起来像

ID Count Enabled Count Disabled
A 2 1
B 1 1

我该怎么做?

【问题讨论】:

    标签: sql


    【解决方案1】:

    您可以使用条件聚合:

    SELECT ID,
           COUNT(CASE WHEN Status = 'Enabled'  THEN 1 END) AS "Count Enabled",
           COUNT(CASE WHEN Status = 'Disabled' THEN 1 END) AS "Count Disabled"
    FROM yourTable
    GROUP BY ID
    ORDER BY ID;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-24
      • 2018-12-26
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多