Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Combining unrelated Selects

$
0
0

My data looks like this:

date                            n1  n2    n3    n4   n5
2013-08-26 00:00:00   12  13    19    27   30
2013-08-25 00:00:00   6    17    21    30   31
2013-08-24 00:00:00   4    17    22    26   29
2013-08-23 00:00:00   3    10    30    31   40
2013-08-22 00:00:00   13  14    20    27   33
2013-08-21 00:00:00   14  22    29    30   33
2013-08-20 00:00:00   8    10    19    25   40
2013-08-19 00:00:00   2    3      9     14    31
2013-08-18 00:00:00   10  16    18    30    37

I have 5 different numbers I need to search for and return the number and date associated with it.

I am using this query which works fine.

DECLARE @NUM1 INT SET @NUM1 = 17

select @NUM1 as N1, date from pick5 where n1=@num1 or n2=@num1 or n3=@num1 or n4=@num1 or n5=@num1

I get result
N1   Date
17   2013-08-25 00:00:00 
17   2013-08-24 00:00:00

Then I run

DECLARE @NUM1 INT
SET @NUM1 = 18
select @NUM1 as N2, date from pick5 where
n1=@num1 or n2=@num1 or n3=@num1 or n4=@num1 or n5=@num1

I get result
N2  Date
18  2013-08-18 00:00:00

I run for all 5 numbers and get 5 result sets.  What I want using the 2 examples above is to have the results combined even though they are not related.  I want to get the results like this
N1    Date                          N2    Date
17    2013-08-25 00:00:00  18    2013-08-18 00:00:00
17    2013-08-24 00:00:00  NULL  NULL

I will be combining 5 results so I will have 10 columns in one result set so I can insert into a table.  Each pair starting at row 1.

The closest I can get now which I dont want is below:
N1      Date                          N2    Date
17      2013-08-25 00:00:00  NULL  NULL
17      2013-08-24 00:00:00  NULL  NULL
NULL  NULL                          18    2013-08-18 00:00:00

Thanks in advance for any help.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>