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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 2 SELECT statements with UNION – how to establish

Author  Topic 

Berduchwal
Starting Member

3 Posts

Posted - 2007-02-16 : 09:40:02
I have two united select statements that are showing errors on a particular day. Errors are calculated:
Table 1 with 3 columns date, value 1 and value 2
Table 2 with 3 columns date, value 1 and value 2

SELECT table1.date, Round(table1.[value 1]-table2.[value 1],2) AS [Diff]
FROM table1 INNER JOIN table2 ON table1.date = table2.date
WHERE (((table1.date)>=#10/1/2006#)
UNION
SELECT table1.date, Round(table1.[value 2]-table2.[value 2],2) AS [Diff]
FROM table1 INNER JOIN table2 ON table1.date = table2.date
WHERE (((table1.date)>=#10/1/2006#)

But this statements returns two columns: 1 date, 2 diff. I want it to display 3 columns: 1 date, 2 diff, 3 which value (1 or 2).
Is this possible at all?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-02-16 : 09:43:53
like this ?

SELECT table1.date, Round(table1.[value 1]-table2.[value 1],2) AS [Diff], 1 as Value
FROM table1 INNER JOIN table2 ON table1.date = table2.date
WHERE (((table1.date)>=#10/1/2006#)
UNION
SELECT table1.date, Round(table1.[value 2]-table2.[value 2],2) AS [Diff], 2 as Value
FROM table1 INNER JOIN table2 ON table1.date = table2.date
WHERE (((table1.date)>=#10/1/2006#)



KH

Go to Top of Page

Berduchwal
Starting Member

3 Posts

Posted - 2007-02-16 : 09:53:21
Thx it works
Go to Top of Page
   

- Advertisement -