简体   繁体   中英

Incorrect GROUP BY usage

Why is this regarded as incorrect GROUP BY usage?

SELECT Empno, Deptno,
SUM(Sal) "Total"
FROM Emp
GROUP BY Deptno;

The result given in my notes is:

 Empno Deptno Total ------- ------ -------- ? 10 8750 ? 20 10875 ? 30 9400 

with the reason of:

All columns in SELECT list must either appear in GROUP BY or be aggregated!

But I'm not too sure what it means...

You're selecting Empno from the results - that's not an aggregate function of several rows, nor is it part of the grouping, therefore it doesn't make sense for it to be in the query. What would you expect the result to be from two rows for different employees in the same department? For example, consider the following data:

Empno    Deptno    Sal
    1         1    100
    2         2    200
    3         2    200

What should the result be?

Since both Empno and Deptno are selected, you need to group by both fields:

SELECT Empno, Deptno, SUM(Sal) "Total"
FROM Emp
GROUP BY Empno, Deptno;

or just select Deptno if Empno isn't used:

SELECT Deptno, SUM(Sal) "Total"
FROM Emp
GROUP BY Deptno;

Try this-

SELECT group_concat(Empno), Deptno,
SUM(Sal) "Total"
FROM Emp
GROUP BY Deptno;

The error message is pretty clear on that: the error is because you have a "naked" employee number on the select list, but you are not grouping by it (which makes sense, assuming that employee number is unique: it would make your group by useless).

If you are looking for the total salary paid to all employees by department, all you need to do is removing the employee number:

SELECT Deptno, SUM(Sal) "Total"
FROM Emp
GROUP BY Deptno

I think what you're looking for is that:

SELECT Empno, Deptno,
SUM(Sal) "Total"
FROM Emp
GROUP BY Empno, Deptno;

You're assuming that Empno is the same for each Deptno so it's not needed to group by this field, but syntactically that's not correct.

Whenh you ask for a grouped result, the fields to be returned should be either part of the group by clause, of an aggregate function.

Why is that?

Imagine this data from your table Emp:

Empno   Deptno
1       1
2       1
3       2
4       2

Whehn you perform your select, there would be two groups (Deptno 1 and 2). Which data should be shown for the Empno in those cases? The engine can't tell.

So, your select could be fixed in different ways:

SELECT Empno, Deptno,
SUM(Sal) "Total"
FROM Emp
GROUP BY Empno, Deptno;

that groups for both Empno and Deptno, or maybe:

SELECT Max(Empno), Deptno,
SUM(Sal) "Total"
FROM Emp
GROUP BY Deptno;

that groups by Deptno, and gives just one Empno (the maximum of the group).

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