gpt4 book ai didi

sql - 一个查询中有两个 WHERE & COUNT

转载 作者:行者123 更新时间:2023-12-05 00:35:49 25 4
gpt4 key购买 nike

我需要根据不同的条件在同一个查询中获得两个求和。两个条件都在同一列上运行。
有没有办法做到这一点?

我可以用一个例子来最好地解释这一点:

Table : salary_survey_result

Columns: industry, location, position, salary



实际上,我想结合以下两个查询:
SELECT industry, location, count(*) as MORE_THAN_SIX_FIGURE   
FROM salary_survey_result
WHERE salary > 100000
GROUP BY industry, location


SELECT industry, location, count(*) as MORE_THAN_FIVE_FIGURE  
FROM salary_survey_result
WHERE salary > 10000
GROUP BY industry, location

所以结果是这样的:
industry location MORE_THAN_FIVE_FIGURE MORE_THAN_SIX_FIGURE
Healthcare    NY               45                      10  
Healthcare MN 35 6
InfoTech NY 50 19
InfoTech MN 40 12

最佳答案

就像是

SELECT industry, 
location,
sum( case when salary >= 10000 then 1 else 0 end) as MORE_THAN_FIVE_FIGURE,
sum( case when salary >= 100000 then 1 else 0 end) as MORE_THAN_SIX_FIGURE
FROM salary_survey_result
WHERE salary >= 10000
GROUP BY industry, location
WHERE salary >= 10000结果不需要子句。如果 SALARY 上有索引,可能会提高性能并且大多数薪水值小于 10000。请注意,我还假设您的意思是 >=而不是 >因为 10,000 的薪水是五位数的薪水。

关于sql - 一个查询中有两个 WHERE & COUNT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8934001/

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