halfwind22
halfwind22

Reputation: 361

SUM(COUNT(*)) in Oracle

I am using the popular employee table in the HR schema of Oracle. Sample data: https://i.sstatic.net/BPp9a.jpg I am trying the find out the count as well as the percentage of the people who have the same salary from that table.

However, I am getting error when I try the below query :

SELECT SALARY, count(*),((count(*)/sum(count(*))))*100 FROM EMPLOYEES
group by salary;

ORA-00937: not a single-group group function
00937. 00000 -  "not a single-group group function"
*Cause:    
*Action:

However ,when I try the below query, I get the desired result.

SELECT SALARY, count(*),(count(*)/(select sum(count(*)) from employees group by salary))*100 FROM EMPLOYEES
group by salary;

Can somebody tell how the achieve my goal using the first query itself? I understand that the count() returns many rows, whereas the sum (count()) returns just 1 row.

Upvotes: 0

Views: 1679

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269503

You are missing the OVER() clause:

SELECT SALARY, count(*),
       count(*) * 100 / sum(count(*)) OVER ()
FROM EMPLOYEES
GROUP BY salary;

Upvotes: 3

Related Questions