gpt4 book ai didi

sql-server-2008 - 按问题分组

转载 作者:行者123 更新时间:2023-12-04 07:13:33 26 4
gpt4 key购买 nike

select 
case status
when 'AR' then 1
when 'AV' then 2
when 'AN' then 3
when 'AD' then 4
when 'AC' then 5
when 'AY' then 6
when 'AH' then 7
else 8
end 'status_order', COUNT(*)
from dn_appeal_request
group by
case status
when 'AR' then 1
when 'AV' then 2
when 'AN' then 3
when 'AD' then 4
when 'AC' then 5
when 'AY' then 6
when 'AH' then 7
else 8
end


有没有办法在不使用select中使用相同case语句的情况下进行分组?

最佳答案

您可以通过使用子查询来避免重复执行case语句。您确实需要对值进行分组。

select D.status_order, COUNT(*)
from (
select case status
when 'AR' then 1
when 'AV' then 2
when 'AN' then 3
when 'AD' then 4
when 'AC' then 5
when 'AY' then 6
when 'AH' then 7
else 8
end 'status_order'
from dn_appeal_request
) as D
group by D.status_order

关于sql-server-2008 - 按问题分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6960718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com