Skip to Main Content
  • Questions
  • Using group function in merge statement

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Jamshaid.

Asked: June 09, 2009 - 1:31 am UTC

Last updated: January 02, 2012 - 8:34 am UTC

Version: 10.2.0

Viewed 10K+ times! This question is

You Asked

Hi Tom,

Just wanted to ask that i want to use group function in merge statement. Is it posible because when i group function in values(col1,col2,count(*)...) statement returns error group function not allowed here. Please explain with example if posible.

Regards

and Tom said...

ops$tkyte%ORA10GR2> merge into dept
  2  using (select deptno, count(*) cnt from emp group by deptno) e
  3  on (dept.deptno = e.deptno)
  4  when matched then update set cnt = e.cnt
  5  /

3 rows merged.

ops$tkyte%ORA10GR2> select * from dept;

    DEPTNO DNAME          LOC                  CNT
---------- -------------- ------------- ----------
        10 ACCOUNTING     NEW YORK               3
        20 RESEARCH       DALLAS                 5
        30 SALES          CHICAGO                6
        40 OPERATIONS     BOSTON



the set you MERGE FROM (the using set) can be aggregated

of course the set you MERGE INTO cannot be aggregated - it would not make "sense", you would be taking many rows and aggregating them into a single row - there isn't any real physical row to update then.

Rating

  (4 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Jamshaid Khalil, June 10, 2009 - 12:58 am UTC


No functions allowed in merge ( "on" statement )?

Madhu Singh G, May 18, 2011 - 4:54 am UTC

Hi Tom,

Hope you are doing good.

I have table_1 -

create table table_1 ( col1 number, col2 number );

table_2 -

create table table_2 ( col3 number, col4 number );

I've inserted some records in both the tables. Now, I want to merge the data in the table_1 into table_2, based on the following condition.
__________________________

merge into table_2 t2
using ( select * from table_1 where col1 > 100 ) t1
on ( t1.col1 = min(t2.col3,t2.col4) )

when matched then

update table_2
set col4 = col2;

_____________________________

This above query gives an error - ORA:00934 - Group function not allowed here.

when this merge statement is modified using the case statement - it works fine.

____________

merge into table_2 t2
using ( select * from table_1 where col1 > 100 ) t1
on ( t1.col1 = case when ( t2.col3 >t2.col4 ) then t2.col4
else t2.col3
end)

when matched then

update table_2
set col4 = col2;


_________________

is there any difference in how the above query functions when compared to the intent of the first query?

Please explain.

Thanks!

Tom Kyte
May 18, 2011 - 10:10 am UTC

min is an aggregate. (a function that needs GROUP BY - which you cannot really do in a merge - besides min only takes a single expression, not a pair of columns)

perhaps you meant to use least - which is a scalar function?

Thanks Tom!

Madhu Singh G, May 19, 2011 - 9:47 am UTC

DOH..!! Sorry!

Least() - I Meant to use exactly that!!

I will ensure I get sober before I trouble you the next time. :)

Thanks Again!


not working for me..

basavaraj, January 02, 2012 - 12:20 am UTC

hi Tom,
i tried the approach mentioned by you.
below is my MERGE statement.

MERGE INTO Rftm_Pidwise_Revenue_Dtls TRG
USING (SELECT SUM(ltd) EFF,project_id pi
       FROM rftm_time_entry_2
       WHERE project_id IN (select pid 
                            from rftm_pid_details
                                where ideal_ref_no_lv ='140014'
                                and version_number= (select max(version_number) from rftm_pid_details))
       AND version_number= (select max(version_number) from rftm_time_entry_2)
       GROUP BY project_id) SRC
ON
(TRG.pid= SRC.pi)
WHEN MATCHED THEN UPDATE
SET TRG.efforts_prior_qtr= SRC.EFF



but the rows are not getting updated. However, when i change the assignment in the SET clause, say i changed it to 1, it works.
but i want to use the sum for each PID coming under the ideal number '140014'.
Tom Kyte
January 02, 2012 - 8:34 am UTC

my car won't start.

when you can tell me why - I'll answer your question.


How could I answer this? How could anyone?

You have a statement that doesn't work - but I don't know what you are trying to do. If I gave you some code and simply said "this code, which doesn't do what I want, doesn't work. How do I make it work?" - what could you say? You don't know what I'm trying to do.

no create table
no insert into
no explanation
no look