我有一个如下所示的数据集:
ID Description Team
A Missing Name; Missing Gender Team1
B Missing Gender Team1
C Missing Name Team2
D Missing Name Team2
即每个 ID 有一行,带有一个文本列(描述),用于标识 ID 的任何问题,以及 ID 所属团队的名称。我设法将其总结如下:
Team Missing Gender Missing Name
Team1 2 1
Team2 0 2
通过使用如下所示的 case 语句:
SELECT
"Team",
sum(case when "DQ Info" like '%Missing Gender%' then 1 ELSE 0 END) as "Missing Gender",
sum(case when "DQ Info" like '%Missing Name%' then 1 ELSE 0 END) as "Missing Name"
FROM
(*)
GROUP BY "Team"
我希望能够总结这些数据,使其看起来像这样:
Description Team1 Team2
Missing Gender 2 0
Missing Name 1 2
我知道一定有办法做到这一点,但我目前正在苦苦挣扎 - 只需在上述字段中使用“描述”而不是“团队”就会得到如下结果:
Description Team1 Team2
Missing Name; Missing Gender 1 0
Missing Gender 1 0
Missing Name 0 2
这不是我所追求的。感谢您提供任何帮助,尽管我会继续修补,所以如果我设法弄清楚,我会更新这篇文章。请注意,这只是数据集的一个示例,而不是它实际上是什么。
编辑:根据 bpgergo 的建议在下面查询。完成一些阅读后,看起来下面会短路,即一旦满足内部案例中的第一个条件,就会终止查询,这意味着它永远无法对一列进行多次计数。很遗憾,但我想我必须坚持我原来的查询。
SELECT
"Description",
sum(case when "Team" like 'Team1' then 1 ELSE 0 END) as "Team1",
sum(case when "Team" like 'Team2' then 1 ELSE 0 END) as "Team2"
FROM(SELECT
"Team",
case when "DQ Info" like '%[Check Role]%' then '[Check Role]'
ELSE case when "DQ Info" like '%[Client deceased]%' then '[Client deceased]'
ELSE case when "DQ Info" like '%[2 Parents not recorded]%' then '[2 Parents not recorded]'
ELSE case when "DQ Info" like '%[Religion not recorded]%' then '[Religion not recorded]'
ELSE case when "DQ Info" like '%[1st Language not recorded]%' then '[1st Language not recorded]'
ELSE case when "DQ Info" like '%[Ethnicity not recorded]%' then '[Ethnicity not recorded]'
ELSE case when "DQ Info" like '%[No Current Worker]%' then '[No Current Worker]'
ELSE case when "DQ Info" like '%[No Current Team]%' then '[No Current Team]'
ELSE case when "DQ Info" like '%[Update Gender]%' then '[Update Gender]'
ELSE case when "DQ Info" like '%[No Plan]%' then '[No Plan]'
ELSE case when "DQ Info" like '%[Update Plan]%' then '[Update Plan]'
END END END END END END END END END END END
as "Description"
FROM(
*
FROM
(*)WORKLOAD)DATA)
GROUP BY "Description"