sql - 有没有其他合乎逻辑或更健壮的方式

标签 sql oracle hibernate select

我有一个 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/

相关文章:

hibernate - Grails与Ms Access的连接

mysql - 单个 SQL 表的聚合计算查询

mysql - 处于一对一关系的父实体和子实体

c# - 在 linq 查询中调用 c# 方法( Entity Framework )

java - AF :panelAccordion partialtrigger on af:selectOneChoice valueChangeListener

sql - 允许角色查询、插入和更新可变数组

java - 没有 if 条件(动态查询)或乱码的Where子句中的PreparedStatement "is null"

java - 如何使用注释保存将列表作为值的 map ?

java - Jackson 在序列化时触发 JPA Lazy Fetching

sql - 是否可以在 SQL 中将多个常量 SELECT 到多个结果集行中?