Is there any other logical or more reliable way

I have a sql query

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 

Is there any other logical or more reliable way to execute the same query (preferably in hql).

Thank you in advance

+4
source share
2 answers

try

 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 
+6
source

Try using the CASE statement

 select count(case when sal between 0 and 1000 then 1 end) count(case when sal between 1000 and 10000 then 1 end) , count(case when sal between 10000 and 100000 then 1 end) from user_details; 

Note : you also have an else keyword that can be placed at the end (if necessary)

For example, see 1. here
2. It meets your requirements.

+3
source

All Articles