Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Database Design and Application Architecture
 join with unions using where ?

Author  Topic 

sqlclarify
Yak Posting Veteran

56 Posts

Posted - 2008-08-07 : 00:09:51
Hello,

Is it possible to join a union with another sql statement.

for example lets say we have:

select FirstName As Names from Namestable
Union
select lastName As Names from LastNamesTable

so this produces the output:
Names
-----
a
b
c
etc.

we want to join this with another attribute which is produced by the lets say the following statement:

select age from AgesTable

We want :

Names | Age
----- ------
a | x
b | y
c | z

How do we write the statement for this using a where clause?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-08-07 : 00:23:48
How do you relate a to x, b to y etc ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-08 : 15:01:13
if you have a relation between AgesTable and each of Namestable,LastNamesTable you can do it with a derived table containing the union.

select * from
(
select FirstName As Names from Namestable
Union
select lastName As Names from LastNamesTable)t
join AgesTable a
on a.linkingcol=t.Names
Go to Top of Page
   

- Advertisement -