我有一个 sql 查询
select count(salary)
from USER_DETAILS
where salary>0 and salary<1000 union all
select count(salary)
from USER_DETAILS
where salary>1000 and salary<10000 union all
select count(salary)
from USER_DETAILS
where salary>10000 and salary<100000
是否有任何其他逻辑或更健壮的方式来执行相同的查询(最好在 hql 中)。
提前致谢
最佳答案
试试这个,
SELECT COUNT(CASE WHEN salary >= 0 and salary <= 1000 THEN salary END) "salary >= 0 and salary <= 1000",
COUNT(CASE WHEN salary >= 1000 and salary <= 10000 THEN salary END) "salary >= 1000 and salary <= 10000",
COUNT(CASE WHEN salary >= 10000 and salary <= 100000 THEN salary END) "salary >= 10000 and salary <= 100000"
from user_details
关于sql - 有没有其他合乎逻辑或更健壮的方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14930756/