我有一个查询,它返回受让人的门票总数:
SELECT DISTINCT case
when ASSIGNEE LIKE '%e474728%' then 'Jason'
when ASSIGNEE LIKE '%e499653%' then 'Alexis'
when ASSIGNEE LIKE '%e509255%' then 'David'
when ASSIGNEE LIKE '%e533676%' then 'KC Lin'
when ASSIGNEE LIKE '%Desktop_Support%' then 'LIKE Desktop Support'
when ASSIGNEE = 'Desktop_Support' then 'Desktop Support (unknown)'
else 'ASSIGNEE UNKNOWN'
End as ASSIGNEE,
COUNT(STATUS) as tickets
FROM techsupport
WHERE STATUS = 'Closed'
AND ASSIGNEE LIKE '%Desktop_Support%'
GROUP BY ASSIGNEE
WHICH产生结果:
ASSIGNEE tickets
Desktop Support (unknown) 981
David 445
Alexis 135
Jason 48
KC Lin 20
ASSIGNEE UNKNOWN 19
KC Lin 18
Alexis 15
Alexis 14
KC Lin 12
David 11
ASSIGNEE UNKNOWN 11
Alexis 10
Alexis 8
ASSIGNEE UNKNOWN 7
Alexis 4
Jason 4
ASSIGNEE UNKNOWN 4
如何组合各种受让人?
这样它显示了每个受让人的合计总数?
ASSIGNEE tickets
Desktop Support (unknown) 981
David 456
Jason 52
KC Lin 50
ASSIGNEE UNKNOWN 41
Alexis 186
谢谢!
答案 0 :(得分:2)
您正在通过ASSIGNEE进行分组,而不是通过案例表达式的结果进行分组(重命名是在分组后执行的)。另外明显是多余的,这里有一种方法可以不重复group by子句中的情况:
SELECT ASSIGNEE, count(status)
FROM (
SELECT case when ASSIGNEE LIKE '%e474728%' then 'Jason'
when ASSIGNEE LIKE '%e499653%' then 'Alexis'
when ASSIGNEE LIKE '%e509255%' then 'David'
when ASSIGNEE LIKE '%e533676%' then 'KC'
when ASSIGNEE LIKE '%Desktop_Support%' then 'LIKE Desktop Support'
when ASSIGNEE = 'Desktop_Support' then 'Desktop Support (unknown)'
else 'ASSIGNEE UNKNOWN'
End as ASSIGNEE
, STATUS
FROM techsupport
WHERE STATUS = 'Closed'
) as X
GROUP BY ASSIGNEE;
答案 1 :(得分:1)
为case
的别名使用其他名称,以区别于原始列名。
SELECT DISTINCT case
when ASSIGNEE LIKE '%e474728%' then 'Jason'
when ASSIGNEE LIKE '%e499653%' then 'Alexis'
when ASSIGNEE LIKE '%e509255%' then 'David'
when ASSIGNEE LIKE '%e533676%' then 'KC'
when ASSIGNEE LIKE '%Desktop_Support%' then 'LIKE Desktop Support'
when ASSIGNEE = 'Desktop_Support' then 'Desktop Support (unknown)'
else 'ASSIGNEE UNKNOWN'
End as ASSIGNEE_GRP,
COUNT(STATUS) as tickets
FROM techsupport
WHERE STATUS = 'Closed'
AND ASSIGNEE LIKE '%Desktop_Support%'
GROUP BY ASSIGNEE_GRP
答案 2 :(得分:0)
SELECT语句的逻辑处理顺序:
如果你知道这一点,你就会理解为什么受让人会被重复。
答案 3 :(得分:-3)
在查询中尝试group by子句。 group by assignee