Evo copy/paste tvog prijedloga:
SQL> SELECT first_name, last_name, MAX(salary) as "moneymaker" FROM employees GROUP BY salary HAVING MAX(salary) > 1;
SELECT first_name, last_name, MAX(salary) as "moneymaker" FROM employees GROUP BY salary HAVING MAX(salary) > 1
*
ERROR at line 1:
ORA-00979: not a GROUP BY expression
u SELECT dijelu povlacis first_name i last_name sto su kolone nad kojima ne radis nikakvu grupnu funkciju. Samim time te dvije kolone moras imati u GROUP BY klauzuli jer nad trecom kolonom koju povlacis radis grupnu funkciju.
Evo da malo smanjim output. Recimo da me zanimaju samo uposlenici cije ime pocinje sa J
SELECT first_name, last_name, salary
FROM employees
WHERE first_name LIKE 'J%';
FIRST_NAME LAST_NAME SALARY
-------------------- ------------------------- ----------
Jennifer Whalen 4400
John Chen 8200
Jose Manuel Urman 7800
Julia Nayer 3200
James Landry 2400
James Marlow 2500
Jason Mallin 3300
John Seo 2700
Joshua Patel 2500
John Russell 14000
Janette King 10000
FIRST_NAME LAST_NAME SALARY
-------------------- ------------------------- ----------
Jonathon Taylor 8600
Jack Livingston 8400
Jean Fleaur 3100
Julia Dellinger 3400
Jennifer Dilly 3600
16 rows selected.
Znaci, ovo mi izbacuje 16 rezultata, bez grupne funkcije.
Medjutim, postoje duplikati i to samo kod first_name.a, John se pojavljuje tri puta, a Jennifer, James i Julia po dva puta.
Ako odradim sada neku grupnu funkciju nad salary, i sa njom povucem i first_name i last_name, opet cu dobiti svih 16 redova:
SQL> edit
Wrote file afiedt.buf
1 SELECT first_name, last_name, MAX(salary)
2 FROM employees
3 WHERE first_name like 'J%'
4* GROUP BY first_name, last_name
SQL> /
FIRST_NAME LAST_NAME MAX(SALARY)
-------------------- ------------------------- -----------
John Chen 8200
Julia Dellinger 3400
Jennifer Dilly 3600
Jean Fleaur 3100
Janette King 10000
James Landry 2400
Jack Livingston 8400
Jason Mallin 3300
James Marlow 2500
Julia Nayer 3200
Joshua Patel 2500
FIRST_NAME LAST_NAME MAX(SALARY)
-------------------- ------------------------- -----------
John Russell 14000
John Seo 2700
Jonathon Taylor 8600
Jose Manuel Urman 7800
Jennifer Whalen 4400
16 rows selected.
U oba ova slucaja, svaki row je identican i tretiran je kao jedna grupa.
Medjutim, ako me last_name ne zanima, hocu samo first_name i MAX(salary), rezultat ce biti drugaciji:
SQL> edit
Wrote file afiedt.buf
1 SELECT first_name, MAX(salary)
2 FROM employees
3 WHERE first_name like 'J%'
4* GROUP BY first_name
SQL> /
FIRST_NAME MAX(SALARY)
-------------------- -----------
Jose Manuel 7800
Jean 3100
John 14000
Julia 3400
Jonathon 8600
Jack 8400
Jennifer 4400
Jason 3300
Joshua 2500
Janette 10000
James 2500
11 rows selected.
U ovom slucaju su identicna imena tretirana kao jedna grupa, tako da su tri Johna dobila jedan entry sa najvecom platom itd.