如何使用嵌套大小写并将结果组合在一起。 这是我的查询:
SELECT COUNT(inc.inc_id) AS event_count,
CASE inc_data.event_type
WHEN 'c' then case inc_data.sub_event_type
when 's' then 'SR' else 'Project'
end
WHEN 'i' then 'incident'
WHEN 'p' then 'Problem'
WHEN 'd' then 'Decision'
WHEN 't' then 'Task'
end "event_sub_type"
FROM inc INNER JOIN inc_data ON inc.inc_id = inc_data.inc_id
GROUP BY inc_data.event_type, inc_data.sub_event_type
返回:
+-------------+----------------+
| event_count | event_sub_type |
+-------------+----------------+
| 5 | Project |
| 10 | Decision |
| 15 | Incident |
| 20 | Problem |
| 25 | Task |
| 30 | SR |
+-------------+----------------+
预期产出:
+-------------+----------------+
| event_count | event_sub_type |
+-------------+----------------+
| 5 | Project |
| 25 | Others |
+-------------+----------------+
如何修改上述查询以获得预期的输出?
答案 0 :(得分:2)
你可以尝试一下吗?
SELECT COUNT(inc.inc_id) AS event_count,
(CASE WHEN (inc_data.event_type = 'c' AND inc_data.sub_event_type <> 's') THEN 'Project' ELSE 'Others' END ) "event_sub_type"
FROM inc INNER JOIN
inc_data ON inc.inc_id = inc_data.inc_id
GROUP BY (CASE WHEN (inc_data.event_type = 'c' AND inc_data.sub_event_type <> 's') THEN 'Project' ELSE 'Others' END )
答案 1 :(得分:0)
怎么样
SELECT COUNT(inc.inc_id) AS event_count,
CASE inc_data.event_type
WHEN 'c' then case inc_data.sub_event_type
when 's' then 'Other' else 'Project'
end
ELSE 'Project'
END "event_sub_type"
FROM inc INNER JOIN inc_data ON inc.inc_id = inc_data.inc_id
GROUP BY inc_data.event_type, inc_data.sub_event_type
答案 2 :(得分:-1)
根据您的输出,我假设您使用的是MySQL。
MySQL允许您按列号分组,因此您可以将GROUP BY子句替换为:
GROUP BY 1, 2