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)
 UNION Query

Author  Topic 

dalmada
Starting Member

12 Posts

Posted - 2006-01-06 : 10:36:05
Hello.

I have a problem

I have a union query i.e.

Select
c as DESCRIPTION
sum(a) as EXECUTED
'0' as PENDING
UNION
c as DESCRIPTION
'0' as EXECUTED
'sum(b) as PENDING

But I Can't seem to find the way to get the results in one line. I need the description and the two columns added. How can I do this? Also, if the description on the first query has the one I want and the second one is NULL how can I use the first one without getting two lines as a result with the description and the NULL value?

Thanks

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-01-06 : 10:53:46
How about some sample data with expected results and a clear description of what you are trying to do? You didn't even give us a valid SELECT to start with, so it is pretty hard to guess what you need. Be specific, and we can help you quickly.
Go to Top of Page

dalmada
Starting Member

12 Posts

Posted - 2006-01-06 : 10:58:16
SELECT
arg.description AS DESCRIPTION,
arg.total1 AS EXECUTED
'0' AS PENDING
from momot.arg arg
UNION
SELECT
arg.description AS DESCRIPTION,
'0' AS EXECUTED
arg.total2 AS PENDING
from momot.arg arg

WHAT I GET RIGHT NOW IS THIS

DESCRIPTION EXECUTED PENDING
RED 45 0
<NULL> 0 54

WHAT I NEED IS

DESCRIPTION EXECUTED PENDING
RED 45 54
Go to Top of Page

dalmada
Starting Member

12 Posts

Posted - 2006-01-06 : 11:12:38
help!
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-01-06 : 11:45:55
wrap your existing SQL like this:


SELECT
MAX(DESCRIPTION) as Description,
SUM(EXECUTED) as Executed,
SUM(PENDING) as Pending
FROM
(your existing SQL) a


That will turn your result from 2 lines into 1.
Go to Top of Page

dalmada
Starting Member

12 Posts

Posted - 2006-01-06 : 11:48:14
didn't work

I'm still getting to lines that look like this

DESCRIPTION EXECUTED PENDING
RED 54 0
RED 0 45
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-01-06 : 14:43:16
What is the SQL statement that you tried?
Go to Top of Page
   

- Advertisement -