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
 New to SQL Server Programming
 Possible to use UNION on rows instead of columns?

Author  Topic 

swims01
Yak Posting Veteran

59 Posts

Posted - 2009-06-08 : 13:48:25
Is it possible to use the UNION command on rows instead of columns?

From what I've gathered you can have two queries with the same amount of columns and use UNION (or UNION ALL) to group the results into one list.

I've got one query with 3 rows of results & 5 columns and another query with 3 rows of results & 1 column.

Is it possible to somehow merge that 1 column from the second query into the other?

If it's not possible or difficult then I'm not too worried about it.

Thanks.

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-06-08 : 14:06:10
You just have to use "place holders'

SELECT col1,col2,col3,col4,col5
FROM table1
UNION
SELECT col1,0,0,0,0 (or col1,null,null,null,null)
FROM table2

As long as the top query and bottmom queries "match up" you can do it.

Jim
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-06-08 : 14:25:44
UNION ALL does union rows (not columns). So that a 3 row result set UNION ALLed with another 3 row result set returns 6 rows. And as Jim says you need to have the same column count and column order from both queries. Perhaps you mean that you want still three rows but with 6 columns? If that is the case then you just need to INNER JOIN the two tables on based on a correlating column.

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -