简体   繁体   中英

Oracle SQL Throws Error for Using Analytic function along with List agg Group by

Getting error in the below code where I use a combination of Sum analytic function along with LISTAGG of Group By combination.

When I run this Analytic part alone, it runs fine (where I dont use LISTAGG & GROUP BY)

SELECT
NUM,
SUM(COLLECTION) OVER(PARTITION BY STATUS ORDER by DT) AS TOTL_STUS
FROM
(select '1' AS NUM,'AA' NAME,'AC' AS STATUS, '10' AS COLLECTION, 
 TO_DATE('06/09/2017','MM/DD/YYYY') AS DT  from dual
UNION ALL
select '2' AS NUM,'DC' NAME,'CL' AS STATUS, '100' AS COLLECTION, 
TO_DATE('06/09/2017','MM/DD/YYYY')-2 AS DT  from dual
UNION ALL
select '3' AS NUM,'MA' NAME,'PE' AS STATUS, '110' AS COLLECTION, 
TO_DATE('06/09/2017','MM/DD/YYYY')-10 AS DT  from dual
UNION ALL
select '1' AS NUM,'AA' NAME,'DS' AS STATUS, '200' AS COLLECTION, 
TO_DATE('06/09/2017','MM/DD/YYYY')+5 AS DT  from dual
UNION ALL
select '2' AS NUM,'DC' NAME,'AC' AS STATUS, '1000' AS COLLECTION, 
TO_DATE('06/09/2017','MM/DD/YYYY') AS DT  from dual
)
;

When I use LISTAGG & GROUP BY it Runs fine (No Analytic column)

SELECT
NUM,
regexp_replace(listagg(STATUS, ',') within group (order by DT) ,'([^,]+)(,\1)*(,|$)', '\1\3')AS UNDUP_STATS
FROM
(select '1' AS NUM,'AA' NAME,'AC' AS STATUS, '10' AS COLLECTION, 
 TO_DATE('06/09/2017','MM/DD/YYYY') AS DT  from dual
UNION ALL
select '2' AS NUM,'DC' NAME,'CL' AS STATUS, '100' AS COLLECTION, 
TO_DATE('06/09/2017','MM/DD/YYYY')-2 AS DT  from dual
UNION ALL
select '3' AS NUM,'MA' NAME,'PE' AS STATUS, '110' AS COLLECTION, 
TO_DATE('06/09/2017','MM/DD/YYYY')-10 AS DT  from dual
UNION ALL
select '1' AS NUM,'AA' NAME,'DS' AS STATUS, '200' AS COLLECTION, 
TO_DATE('06/09/2017','MM/DD/YYYY')+5 AS DT  from dual
UNION ALL
select '2' AS NUM,'DC' NAME,'AC' AS STATUS, '1000' AS COLLECTION, 
TO_DATE('06/09/2017','MM/DD/YYYY') AS DT  from dual
)
GROUP BY NUM
;

But I combine both and Run .

SELECT
NUM,
SUM(COLLECTION) OVER(PARTITION BY STATUS ORDER by DT) AS TOTL_STUS,
regexp_replace(listagg(STATUS, ',') within group (order by DT) ,'([^,]+)(,\1)*(,|$)', '\1\3')AS UNDUP_STATS
FROM
(select '1' AS NUM,'AA' NAME,'AC' AS STATUS, '10' AS COLLECTION, 
 TO_DATE('06/09/2017','MM/DD/YYYY') AS DT  from dual
UNION ALL
select '2' AS NUM,'DC' NAME,'CL' AS STATUS, '100' AS COLLECTION, 
TO_DATE('06/09/2017','MM/DD/YYYY')-2 AS DT  from dual
UNION ALL
select '3' AS NUM,'MA' NAME,'PE' AS STATUS, '110' AS COLLECTION, 
TO_DATE('06/09/2017','MM/DD/YYYY')-10 AS DT  from dual
UNION ALL
select '1' AS NUM,'AA' NAME,'DS' AS STATUS, '200' AS COLLECTION, 
TO_DATE('06/09/2017','MM/DD/YYYY')+5 AS DT  from dual
UNION ALL
select '2' AS NUM,'DC' NAME,'AC' AS STATUS, '1000' AS COLLECTION, 
TO_DATE('06/09/2017','MM/DD/YYYY') AS DT  from dual
)
GROUP BY NUM
;

Below is the sample query which throws error.

  ORA-00979: not a GROUP BY expression
  00979. 00000 -  "not a GROUP BY expression"
  *Cause:    
  *Action:
  Error at Line: 3 Column: 5

Can someone help in understanding this error reason and solve the issue.

What result do you expect from your query? Your analytic sum partitions by status , not by num , so - regardless of ANY code - it is not clear what you are trying to achieve. Are you sure you have the correct logic?

A query is evaluated in the following order: the FROM clause generates the rows to work on. This part is clear in your example. Then - JOIN and WHERE conditions are evaluated. (There are none in your example.) Then the groups requested in the GROUP BY clause are formed. Only at this point the SELECT clause is evaluated, and if you do have a GROUP BY clause, then the SELECT clause can only contain columns (or, more generally, expressions) included in GROUP BY, constant expressions, aggregate functions, and expressions built from these.

In your example, after you group by NUM, what do you mean by "status" in the partition by clause, or by dt in the order by clause, of the analytic sum? They don't exist anymore. Moreover, since you are attempting to use an analytic function (not an aggregate function), the argument collection must be a "group by" expression too, and it isn't. The error message points to collection as causing the error, but so would the other two.

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