Home » SQL & PL/SQL » SQL & PL/SQL » combining different column names from different queries
combining different column names from different queries [message #261251] Wed, 22 August 2007 03:56 Go to next message
Abeer30
Messages: 110
Registered: March 2007
Senior Member
hello,

if i have 2 queries:
select table1.field1,table2.fieled 2 as "one"
from table1,table2
where
table1.pk=table2.fk
and table1.fieldx=??

select table1.field1,table2.fieled 2 as "two"
from table1,table2
where
table1.pk=table2.fk

how can i combine the results of two queries?

Please help me...
Re: combining different column names from different queries [message #261274 is a reply to message #261251] Wed, 22 August 2007 04:31 Go to previous messageGo to next message
JRowbottom
Messages: 5933
Registered: June 2006
Location: Sunny North Yorkshire, ho...
Senior Member
That depends entirely on what format you want the output to be in.

Do you want 2 or 3 columns in the output?
Re: combining different column names from different queries [message #261275 is a reply to message #261251] Wed, 22 August 2007 04:44 Go to previous messageGo to next message
Michel Cadot
Messages: 64132
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:
how can i combine the results of two queries?

Use "union all" between them.

Regards
Michel
Re: combining different column names from different queries [message #261291 is a reply to message #261274] Wed, 22 August 2007 05:00 Go to previous messageGo to next message
Abeer30
Messages: 110
Registered: March 2007
Senior Member
hello,

I want the three columns please
Re: combining different column names from different queries [message #261311 is a reply to message #261291] Wed, 22 August 2007 05:27 Go to previous messageGo to next message
Maaher
Messages: 7062
Registered: December 2001
Senior Member
It doesn't make sense: query 2 incorporates the results from query 1. If you have result 2 then you have result 1 too (as part of result 2).

MHE
Re: combining different column names from different queries [message #261349 is a reply to message #261291] Wed, 22 August 2007 06:32 Go to previous messageGo to next message
Michel Cadot
Messages: 64132
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
If I understand what you want:
select col1, col2 "one", null "two"
from ...
union all
select col1, null "one", col2 "two"
from ...

Regards
Michel

Re: combining different column names from different queries [message #264929 is a reply to message #261349] Wed, 05 September 2007 01:11 Go to previous messageGo to next message
danishayder
Messages: 5
Registered: September 2006
Location: Karachi
Junior Member

hi,
Only possible by union operator but be sure Data Type must be same
and use union instead of union All.

Regards
Danish Hayder
Re: combining different column names from different queries [message #264943 is a reply to message #261251] Wed, 05 September 2007 01:43 Go to previous messageGo to next message
Abeer30
Messages: 110
Registered: March 2007
Senior Member
hello,

i had the required results with the following select technique:

select a.field1,b.field2,b.field3,
(select c.field4 as "column A"
from table1 c
, table2 d
where c.pk=d.fk)
,(select c.field4 as "column B"
from table1 c
, table2 d
where c.pk=d.fk)
from
table1 a
, table2 b
where a.pk=b.fk

thank you...

[Updated on: Wed, 05 September 2007 01:45]

Report message to a moderator

Re: combining different column names from different queries [message #264947 is a reply to message #264943] Wed, 05 September 2007 01:46 Go to previous message
Maaher
Messages: 7062
Registered: December 2001
Senior Member
Please edit your post. I'm getting tired of formatting each post we get. Your SQL should be readable. It will help you in getting a quick reply. Wink More tips? Check out the Forum Guidelines.
Abeer30 wrote on Wed, 05 September 2007 08:43
thank you...
You're welcome.

MHE
Previous Topic: Help needed with Uni Assignment PLEASE!!!!
Next Topic: regarding mutating error
Goto Forum:
  


Current Time: Wed Dec 07 18:19:14 CST 2016

Total time taken to generate the page: 0.14756 seconds