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
 Union Query problem

Author  Topic 

mkgmahi
Starting Member

24 Posts

Posted - 2011-12-19 : 00:17:55
I have a problem with the union query.

How should i add a column in a union query without affecting the rows

For Example:

select a from x union select b from y

Both queries have similar records so they unite and show me without dups.

But

select a, ' ' status from x union select, status from Y

Wherein first query we give comment as we dont have status in it and in the second query we add the status but this time it shows me additional records which is not tallying with my previous no of rows.

Hope u guys can understand my explanation

Please let me know any suggestions..it would be great

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-12-19 : 00:22:14
[code]
select a, isnull(y.status, '') as status
from
(
select a from x
union
select b from y
) z
left join y on z.a = y.b
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

mkgmahi
Starting Member

24 Posts

Posted - 2011-12-19 : 00:41:49
I tried but i am unable to get the result.

select
t.type,
t.qty,
' ' status
from
type t
union
select
t.store
t.qty,
i.status
from
type t,
inventry i

Please let me know for the above one.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-19 : 01:09:43
if you want to tally number of rows, then you need to determine how you're going to merge comments with statuses. do you've common column on basis of which you can merge? else how would you determine which comment gets merged with which status?


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-12-19 : 01:54:35
quote:
Originally posted by mkgmahi

I tried but i am unable to get the result.

select
t.type,
t.qty,
' ' status
from
type t
union
select
t.store
t.qty,
i.status
from
type t,
inventry i

Please let me know for the above one.



Is that the query that you tried ? Have you tried with the query that i posted ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

mkgmahi
Starting Member

24 Posts

Posted - 2011-12-19 : 07:03:35
sorry for late reply... I tried it but i am not getting the result
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-19 : 10:48:01
quote:
Originally posted by mkgmahi

sorry for late reply... I tried it but i am not getting the result


did you read my question? any answers...

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -