简体   繁体   中英

select sum(columnName) from TableName groupby columnName , how i can get same result without using group by

Table Name:emp

Salary      Designation 
3000            A
4000            B
5000            C
2000            A
1000            B

sql>select sum(salary) from emp group by designation;

Result 
A  5000
B  5000
C  5000

Question: how i can get same result without using group by.

Have you tried using a debugger to see the value of data at the time of the println statement?

Also, Why not change the if(sc.hasNext()) to while(sc.hasNext()) to print everything from the scanner?

If the scanner contains X number of lines, then you need to call nextLine() X number of times to read all of the lines. The if statement only calls nextLine() once, and the while statement calls nextLine() until sc.hasNext() is false, which is X times.

Why would you want to do this without GROUP BY ? I see no sense in this. Anyway, you can do this by selecting the distinct designations first and then get their salary sums in a subquery:

select (select sum(salary) from designations d2 where d2.designation = d.designation)
from (select distinct designation from designations) d;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM