Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Nested group function without GROUP BY

ToshishSep 2 2015 — edited Sep 4 2015

Hallo everybody!

I have a table with history of changes. The table has the following structure:

key_valuerow_iddtnew_value
922131AAA7fnAAMAAH6IEACI20.05.201573
922131AAA7fnAAMAAH6IEACI19.05.201519
922131AAA7fnAAEAAKCrMABo18.05.2015
1238040AAA7fnAAEAAKCrMABq20.05.201545
1238040AAA7fnAAEAAKCrMABq21.05.201512
1245506AAA7fnAAEAAKCrMABr15.07.20152
1281570AAA7fnAAEAAKCrMABx11.05.2015
922131AAA7fnAAMAAH6IEACb19.05.201520

key_value - is an object

row_id - is object's attribute

dt - date of changing

new_value - value of attribute after changing

The task is to find values of attributes, actual for determined date, and to aggregate them into one text field for each key_value.

In given example I am expecting the following:

:dt = 20.05.2015

------------------------------------------------

922131       20;73

1238040     45

1281570 

I try the following select:

with a

as

(select 1245506 as key_value, 'AAA7fnAAEAAKCrMABr' as row_id, to_date('15.07.2015','dd.mm.yyyy') as dt, 2 as new_value from dual union all

select 922131, 'AAA7fnAAMAAH6IEACI', to_date('19.05.2015','dd.mm.yyyy'), 19 from dual union all

select 922131, 'AAA7fnAAMAAH6IEACI', to_date('20.05.2015','dd.mm.yyyy'), 73 from dual union all

select 1238040, 'AAA7fnAAEAAKCrMABq', to_date('20.05.2015','dd.mm.yyyy'), 45 from dual union all

select 922131, 'AAA7fnAAEAAKCrMABo', to_date('18.05.2015','dd.mm.yyyy'), null from dual union all

select 1281570, 'AAA7fnAAEAAKCrMABx', to_date('11.05.2015','dd.mm.yyyy'), null from dual union all

select 922131, 'AAA7fnAAMAAH6IEACb', to_date('19.05.2015','dd.mm.yyyy'), 20 from dual union all

select 1238040, 'AAA7fnAAEAAKCrMABq', to_date('21.05.2015','dd.mm.yyyy'), 12 from dual)

SELECT

            key_value, wm_concat(MAX (new_value)

               KEEP (DENSE_RANK LAST ORDER BY a.dt)

               OVER (PARTITION BY row_id) )

    FROM a

   WHERE  a.dt <= :dt

But it gives an error: ORA-00978 nested group function without GROUP BY

When I try:

with a

as

(select 1245506 as key_value, 'AAA7fnAAEAAKCrMABr' as row_id, to_date('15.07.2015','dd.mm.yyyy') as dt, 2 as new_value from dual union all

select 922131, 'AAA7fnAAMAAH6IEACI', to_date('19.05.2015','dd.mm.yyyy'), 19 from dual union all

select 922131, 'AAA7fnAAMAAH6IEACI', to_date('20.05.2015','dd.mm.yyyy'), 73 from dual union all

select 1238040, 'AAA7fnAAEAAKCrMABq', to_date('20.05.2015','dd.mm.yyyy'), 45 from dual union all

select 922131, 'AAA7fnAAEAAKCrMABo', to_date('18.05.2015','dd.mm.yyyy'), null from dual union all

select 1281570, 'AAA7fnAAEAAKCrMABx', to_date('11.05.2015','dd.mm.yyyy'), null from dual union all

select 922131, 'AAA7fnAAMAAH6IEACb', to_date('19.05.2015','dd.mm.yyyy'), 20 from dual union all

select 1238040, 'AAA7fnAAEAAKCrMABq', to_date('21.05.2015','dd.mm.yyyy'), 12 from dual)

SELECT

            key_value, wm_concat(MAX (new_value)

               KEEP (DENSE_RANK LAST ORDER BY a.dt)

               OVER (PARTITION BY row_id) )

    FROM a

   WHERE a.dt <= :dt

GROUP BY key_value

It gives: ORA-00979 not a GROUP BY expression (for MAX (new_value))

How can I solve the task?

We have Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi, so I can't use LISTAGG. Actually, I use user-defined functions,  wm_concat was given just as an example. I receive the same errors with any other aggregate function (e.g. COLLECT).

Сообщение отредактировано: Toshish

This post has been answered by Etbin on Sep 3 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 2 2015
Added on Sep 2 2015
15 comments
2,819 views