SQL 查询排序和获取唯一计数
SQL Query for sorting and getting unique count
我有一个 table,其中包含以下详细信息
Customer
Deal
DealStage
A
D1
Lost
A
D2
Won
A
D3
Contacted
B
D4
Conatcted
B
D5
Lost
C
D6
Lost
D
D7
Lost
我必须开发一个查询,我应该在其中为每个客户获取唯一的最高阶段。阶段优先级为赢得 > 联系 > 失败。例如,A 有三笔交易,分别是 Won、Lost 和 Contacted。所以我应该考虑赢。同样联系 B 和丢失 C 和 D
是否有可能得到像
这样的输出
Customer
Highets Stage
A
Won
B
Contacted
C
Lost
D
Lost
通过这个,我可以生成一个看起来像
的枢轴table
Stage
CustomerCount
Won
1
Contacted
1
Lost
2
提前致谢
使用windows函数如下:
select * from
(select t.*,
row_number() over (partition by customer
order by case when dealstage = 'Won' then 1
when dealstage = 'Contacted' then 2
when dealstage = 'Lost' then 3
end
) as rn
from your_table t)
where rn = 1;
一个选项使用聚合和 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
这实际上是两个不同的问题。事实上,我会推荐两种不同的方法。首先,条件聚合:
select customer,
coalesce(max(case when state = 'Won' then state end),
max(case when state = 'Contacted' then state end),
max(case when state = 'Lost' then state end)
) as biggest_state
from t
group by customer;
但是,对于您的最终结果,我建议使用相关子查询:
select t.state, count(*)
from t
where t.state = (select t2.state
from t2
where t2.customer = t.customer
order by field(state, 'Won', 'Contact', 'Lost')
limit 1
)
group by t.state;
注意:这里假定原始数据没有重复行。如果是,那么 count(distinct)
是一个调整。
我有一个 table,其中包含以下详细信息
Customer | Deal | DealStage |
---|---|---|
A | D1 | Lost |
A | D2 | Won |
A | D3 | Contacted |
B | D4 | Conatcted |
B | D5 | Lost |
C | D6 | Lost |
D | D7 | Lost |
我必须开发一个查询,我应该在其中为每个客户获取唯一的最高阶段。阶段优先级为赢得 > 联系 > 失败。例如,A 有三笔交易,分别是 Won、Lost 和 Contacted。所以我应该考虑赢。同样联系 B 和丢失 C 和 D
是否有可能得到像
这样的输出Customer | Highets Stage |
---|---|
A | Won |
B | Contacted |
C | Lost |
D | Lost |
通过这个,我可以生成一个看起来像
的枢轴tableStage | CustomerCount |
---|---|
Won | 1 |
Contacted | 1 |
Lost | 2 |
提前致谢
使用windows函数如下:
select * from
(select t.*,
row_number() over (partition by customer
order by case when dealstage = 'Won' then 1
when dealstage = 'Contacted' then 2
when dealstage = 'Lost' then 3
end
) as rn
from your_table t)
where rn = 1;
一个选项使用聚合和 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
这实际上是两个不同的问题。事实上,我会推荐两种不同的方法。首先,条件聚合:
select customer,
coalesce(max(case when state = 'Won' then state end),
max(case when state = 'Contacted' then state end),
max(case when state = 'Lost' then state end)
) as biggest_state
from t
group by customer;
但是,对于您的最终结果,我建议使用相关子查询:
select t.state, count(*)
from t
where t.state = (select t2.state
from t2
where t2.customer = t.customer
order by field(state, 'Won', 'Contact', 'Lost')
limit 1
)
group by t.state;
注意:这里假定原始数据没有重复行。如果是,那么 count(distinct)
是一个调整。