我有一张表格,其中包含以下详细信息
顾客 | 交易 | 交易阶段 |
---|---|---|
一种 | D1 | 丢失的 |
一种 | D2 | 韩元 |
一种 | D3 | 联系方式 |
乙 | D4 | 联系方式 |
乙 | D5 | 丢失的 |
C | D6 | 丢失的 |
D | D7 | 丢失的 |
我必须开发一个查询,我应该为每个客户获得唯一的最高阶段。阶段优先级是 Won > Contacted > Lost。例如,A 有三笔交易,分别是 Won、Lost 和 Contacted。所以我应该考虑Won。同样联系 B 和丢失 C 和 D
是否有可能获得类似的输出
顾客 | 高台舞台 |
---|---|
一种 | 韩元 |
乙 | 联系方式 |
C | 丢失的 |
D | 丢失的 |
通过这个,我可以生成一个看起来像的数据透视表
阶段 | 客户数量 |
---|---|
韩元 | 1 |
联系方式 | 1 |
丢失的 | 2 |
提前致谢
一种选择使用聚合和field()
:
select customer,
case min(field(deal_stage, 'Won', 'Contacted', 'Lost'))
when 1 then 'Won'
when 2 then 'Contacted'
when 3 then 'Lost'
end as highest_stage
from mytable
group by customer
实际上我们可以将其与elt()
:
select customer,
elt(
min(field(deal_stage, 'Won', 'Contacted', 'Lost')),
'Won', 'Contacted', 'Lost'
) as highest_stage
from mytable
group by customer
然后,您可以使用另一个级别的聚合生成最终结果:
select highest_stage, count(*)
from (
select customer,
elt(
min(field(deal_stage, 'Won', 'Contacted', 'Lost')),
'Won', 'Contacted', 'Lost'
) as highest_stage
from mytable
group by customer
) t
group by highest_stage
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句