Home » SQL & PL/SQL » SQL & PL/SQL » merging two queries to create a table (sql)
merging two queries to create a table [message #635158] Mon, 23 March 2015 09:13 Go to next message
palpali
Messages: 138
Registered: December 2014
Location: India
Senior Member
Hallo all..

I would like to share my Problem with you all. Smile
well, I am trying to merge two different queries to create a table from the Output of These two queries.
my first question is:
1) How can i do this? is that correct where i have used UNION ALL? or is this any other methods that i can do?

Others:
1) the both tables have the same column Name
2) and in my final Output, i would like to have final sum of rate1 and rate2 from the both tables
i.e (test1.sum_rate1 + test12.sum_rate1) as Final_sum1
and (test1.sum_rate2 + test12.sum_rate2) as Final_sum2
can you please help me, how can i do this?
thanking you in advance

regards,


my test code:
create table test1 (
        pid Number,
        gid Number,
        rate1 Number,
        rate2 Number
    );

insert into test1 (pid, gid, rate1, rate2) values (20,105,23,25);
insert into test1 (pid, gid, rate1, rate2) values (21,106,12,20);
insert into test1 (pid, gid, rate1, rate2) values (20,105,10,23);
insert into test1 (pid, gid, rate1, rate2) values (24,109,20,30);             
insert into test1 (pid, gid, rate1, rate2) values (24,109,22,21); 
commit;
  
create table test12 (
        pid Number,
        gid Number,
        rate1 Number,
        rate2 Number
    );

insert into test12 (pid, gid, rate1, rate2) values (20,105,10,15); 
insert into test12 (pid, gid, rate1, rate2) values (24,109,12,10);     
insert into test12 (pid, gid, rate1, rate2) values (27,115,10,15);     
insert into test12 (pid, gid, rate1, rate2) values (20,105,5,15);     
insert into test12 (pid, gid, rate1, rate2) values (24,109,5,5); 

commit;


create table test3 as (
select test1.pid, test1.gid, sum(test1.rate1)final_sum1, sum(test1.rate2)final_sum2 from test1 group by pid, gid
union all
select test12.pid, test12.gid, sum(test12.rate1)final_sum1, sum(test12.rate2)final_sum2 from test12 group by pid, gid
);


currently i have Output : (which gives me the sum from both table, but i would like to have just single sum per pid and gid)
PID     GID     Final_sum1  Final_sum2
20	105	33	   48
21	106	12	   20
24	109	42	   51
27	115	10	   15
20	105	15	   30
24	109	17	   15

Re: merging two queries to create a table [message #635159 is a reply to message #635158] Mon, 23 March 2015 09:25 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
SELECT PID, GID, SUM(FINAL_SUM1), SUM(FINAL_SUM2) FROM TABLE3 GROUP BY PID, GID;
Re: merging two queries to create a table [message #635160 is a reply to message #635159] Mon, 23 March 2015 09:34 Go to previous messageGo to next message
palpali
Messages: 138
Registered: December 2014
Location: India
Senior Member
Hallo BlackSwan
Thankx alot for you prompt reply Smile

but how can i use this to create a table? (in the crate table Statement?)
I mean, i would like to have just the final sum value in the test3 table.

and it should give the final Output what i get from your query.. it's my final Output Smile
Re: merging two queries to create a table [message #635163 is a reply to message #635160] Mon, 23 March 2015 09:38 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
You are free to write any SQL that satisfies the requirements.
Please proceed to do so.
Re: merging two queries to create a table [message #635164 is a reply to message #635163] Mon, 23 March 2015 09:47 Go to previous messageGo to next message
palpali
Messages: 138
Registered: December 2014
Location: India
Senior Member

Well, i really don't understand what you mean.. :/ sorry..

but i wouild like to have the ouput of the two queries (from test1 and test12) into test3 (will be Input for test3) and the test3 should give me Output as follow: (as per your select Statement)
but my question is, how can i write this Statement in my following create table Statement to get same Output?
thanking you again

create table test3 as (
select test1.pid, test1.gid, sum(test1.rate1)final_sum1, sum(test1.rate2)final_sum2 from test1 group by pid, gid
union all
select test12.pid, test12.gid, sum(test12.rate1)final_sum1, sum(test12.rate2)final_sum2 from test12 group by pid, gid

);


and expected Output:
PID   GID    Sum(final_sum1)  Sum(final_sum2)
27	115	10	      15
20	105	48	      78
21	106	12	      20
24	109	59	      66


Re: merging two queries to create a table [message #635166 is a reply to message #635164] Mon, 23 March 2015 10:02 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

select pid, gid, sum(final_sum1), sum(final_sum2)
from (your query)
group by pid, gid
/
Re: merging two queries to create a table [message #635167 is a reply to message #635166] Mon, 23 March 2015 10:13 Go to previous messageGo to next message
palpali
Messages: 138
Registered: December 2014
Location: India
Senior Member
sry Michel ..

actually, i am still not having my solution for my Problem.. Sad
sorry to bother you..

once again would like to clear regarding my Problem is:
i have two queries from two different tables (test1 and test12) the Output of these two queries would be the Input for third table and , the rate1 and rate2 field in the third table should have sum of both table respectively (i.e. rate1 from test1 and test12 as well as rate2 from test1 and test12).
and i m looking a Statement for creating a table with this condition above Sad
thankx in advance

regards...
Re: merging two queries to create a table [message #635168 is a reply to message #635167] Mon, 23 March 2015 10:16 Go to previous messageGo to next message
cookiemonster
Messages: 13975
Registered: September 2008
Location: Rainy Manchester
Senior Member
Michel's query does the job as far as I can see, obviously you need to add the create table keywords and your previous query in the middle.
Re: merging two queries to create a table [message #635169 is a reply to message #635168] Mon, 23 March 2015 10:21 Go to previous messageGo to next message
palpali
Messages: 138
Registered: December 2014
Location: India
Senior Member
Hallo all ..
Thankyou very much for you Input and Suggestion.. Smile
sry i did not understand first.. but now i got it and worked it fine Smile


And also i would like to ask you.. would be okey when i use UNION ALL to join These two different quereies? or is there any other solution?

regrads,
Re: merging two queries to create a table [message #635172 is a reply to message #635169] Mon, 23 March 2015 11:13 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

If there are no relations between the 2 queries (but the column numbers and types) then there is no (valuable) other way.

Re: merging two queries to create a table [message #635184 is a reply to message #635172] Mon, 23 March 2015 16:04 Go to previous messageGo to next message
palpali
Messages: 138
Registered: December 2014
Location: India
Senior Member
yes, the columns number and types are the same in both table
and there is no relation between two queries but the both table have the same columns and the primary keys, and as well in the third output table has also the same columns and primary keys.

Would it be not good to use UNION ALL when both table have the same columns and Primary Keys? if so, is there any other methods to merge two queries to get the final output as input for the third table?


thnkx
Re: merging two queries to create a table [message #635186 is a reply to message #635184] Tue, 24 March 2015 01:22 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Is there anything fuzzy in what I said?

Re: merging two queries to create a table [message #635187 is a reply to message #635186] Tue, 24 March 2015 02:06 Go to previous messageGo to next message
palpali
Messages: 138
Registered: December 2014
Location: India
Senior Member

Because I did not understand what you really mean.. that's why i have asked again.. and because i would like to the difference/effect of doing so or is there any other way and so...

sry for that

regards,
Re: merging two queries to create a table [message #635188 is a reply to message #635187] Tue, 24 March 2015 02:34 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
there is no (valuable) other way.


There are ALWAYS different ways to write a query but most if them are not to be used.

Re: merging two queries to create a table [message #635197 is a reply to message #635188] Tue, 24 March 2015 07:12 Go to previous message
palpali
Messages: 138
Registered: December 2014
Location: India
Senior Member
Thank you very much ....
Previous Topic: DBMS_JOB TO run every saturday starting next saturday
Next Topic: Query help
Goto Forum:
  


Current Time: Thu Aug 27 03:23:59 CDT 2026