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 2000 Forums
 SQL Server Development (2000)
 select value from first query in UNION

Author  Topic 

dalmada
Starting Member

12 Posts

Posted - 2006-01-06 : 12:41:05
how can I select the value for the first query in the UNION to always appear no matter what the second query throws?

Example
select description, sum(data) from
(select a.description as description, a.data as data
UNION
select b.description as description, b.data as data)
group by description

nr
SQLTeam MVY

12543 Posts

Posted - 2006-01-06 : 12:46:03
It will always appear - the second query will add values it won't cause them to be dropped.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

dalmada
Starting Member

12 Posts

Posted - 2006-01-06 : 12:53:38
DBut the thing is the second query has blank values so it adds a new line with the description in blank, so now I have two lines instead of one
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-01-06 : 12:55:18
Yep - you still have the value from the first query which is what you want.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

dalmada
Starting Member

12 Posts

Posted - 2006-01-06 : 12:59:30
I want one line with all the info...

Not two

With the description from the first query
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-01-06 : 13:26:28
If you only want one row then you shuoldn't be grouping by description as that is only useful if you have multiple descriptions and want that to match the values from the two queries.

select max(description), sum(data) from
(
select a.description, a.data from a
UNION
select '', b.data from b
) a


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -