sql server 2008 - Joining sql statement in store procedure -
i trying join these 2 statements in stored procedure. how 1 table, can use groupby column.
select tablea.name, tableb.occupation, 'group1' [groupby] tablea, tableb tablea.id = 1 select tablea.name, tableb.occupation, 'group2' [groupby] tablea, tableb tableb.id = 10
my result should this
name occupation groupby david doctor group1 john pilot group1 dwayne wrestler group2 axel rockstar group2
update
table id name occupation 1 david doctor 1 john pilot 2 mike clerk table b id name occupation 3 wayne writer 4 shane publisher
10 dwayne wrestler 10 axel rockstar
it seems can use union all
result:
select tablea.name, tablea.occupation, 'group1' [groupby] tablea tablea.id = 1 union select tableb.name, tableb.occupation, 'group2' [groupby] tableb tableb.id = 10
see sql fiddle demo. union all
return rows between 2 queries. if think might have duplicates want remove, can use union
.
Comments
Post a Comment