简体   繁体   中英

Oracle 11g group by

I'm new to Oracle, and know a little bit about MySQL. I would like to make this work:

SELECT A1, A2, A3, SUM(A4) SA4
FROM (
SELECT A1
    , A2
    , A3
    , A4
FROM T1
GROUP BY A1, A2, A3, A4, A5, A6, A7, A9, A10, A11, A12 ...
) a
GROUP BY A1, A2, A3

I'm pretty sure it would be working in MySQL, but in Oracle I'm getting the following error:

ORA-00979 "not a GROUP BY expression"

It drives me crazy. The inner query is way too big to show here, but there are definitely duplicates, and I that's what I would like to eliminate with an outer query. Can you please help me out what am I doing wrong? Thank you very much!

You do need the group by for the inner query, so do as

    SELECT a1,
         a2,
         a3,
         SUM (a4) sa4
    FROM (SELECT DISTINCT NULL a1,
                          NULL a2,
                          NULL a3,
                          NULL a4
            FROM t1) a
GROUP BY a1, a2, a3

In the inner query:

SELECT DISTINCT A1
    , A2
    , A3
    , A4
FROM T1
GROUP BY A1, A2, A3

You have A4 that is not either part of the GROUP BY expression and also not part of an aggregation. That is what is giving your error.

Change your query to:

SELECT A1, A2, A3, SUM(A4) SA4
FROM (
  SELECT DISTINCT A1, A2, A3, A4
  FROM   T1
) a
GROUP BY A1, A2, A3

However, you can simplify this (getting rid of the inner query) to:

SELECT A1, A2, A3, SUM(DISTINCT A4) SA4
FROM   T1
GROUP BY A1, A2, A3

try this:

SELECT A1, A2, A3, SUM(A4) SA4
FROM (
SELECT 
 DISTINCT 
      A1
    , A2
    , A3
    , A4
FROM T1 
) a
GROUP BY A1, A2, A3

you do not need to use DISTINCT and GROUP by together

The main question is: what do you want to achieve really?

In the inner query you group by A1, A2, A3, so you get a row per A1/A2/A3 combination. All these columns are in the select list. So every row is different from the other, because they all have different A1/A2/A3 combinations. One uses DISTINCT to remove duplicates from the results, but in your case there can be no duplicates as mentioned, so DISTINCT is completely superfluous here.

Then you select A4, but as you group by A1, A2, A3, you'd usually have to decide which of their A4 you want.

  • If A4 is dependent on A1, A2, A3 or a combination thereof (such as is the employee name on the employee number, ie an employee identified by their number only has one name), we are talking about the one A4 and the query would be valid according to the SQL standard. MySQL let's you write the query thus, but Oracle unfortunately still insists on specifying which A4 you want for the group. Well, if there can be just one, you can use MIN(A4) or MAX(A4) to satisfy Oracle here.

  • If however A4 is not dependent on A1, A2, A3 or a combination thereof (such as the employee number is not dependent on the department, because there exists multiple employees per department), then the query is invalid. MySQL, however used to allow this query and would return an A4 arbitrarily chosen (just like arbitrarily picking one of the employees for a department). This often led beginners to write incorrect queries.

At last you group by A1, A2, A3 again, which is completely superfluous, because the data is already grouped by A1, A2, A3. You sum the one A4 value per group, which is well, the value itself of course.

It may be you simply want the A4 sum per A1, A2, A3, which would be

SELECT A1, A2, A3, SUM(A4) AS SA4
FROM T1
GROUP BY A1, A2, A3;

But maybe you want something else altogether.

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