sql server - Merge two SQL SELECT queries results into one query and one result -


i facing problem in merging results of 2 select queries
have executed following code in sql, may try

create table #a(a_id int, is_active bit, cr_date date) create table #b(b_id int, a_id int, cr_date date)  insert #a(a_id,is_active, cr_date) values(1,1,getdate()),(2,0,getdate()),(3,1,getdate()),(4,0,getdate()),(5,0,getdate())  insert #b(b_id,a_id,cr_date) values(1,1,getdate()),(2,1,getdate()),(3,4,getdate()),(4,4,getdate()),(5,4,getdate()),(6,4,getdate())   create table #c(c_id int, is_active bit, cr_date date) create table #d(d_id int, c_id int, cr_date date)  insert #c(c_id,is_active, cr_date) values(1,1,getdate()),(2,0,getdate()),(3,1,getdate()),(4,0,getdate()),(5,0,getdate())  insert #d(d_id,c_id,cr_date) values(1,1,getdate()),(2,1,getdate()),(3,4,getdate()),(4,4,getdate()),(5,4,getdate()),(6,4,getdate())    declare @startdate date; declare @enddate date; set @startdate='2012-10-26'; set @enddate=getdate();  select  '#b= '+cast(count(b.b_id) varchar)   b_count, '#a = '+cast(count(a.a_id) varchar)  a_count   #a left join #b b    on a.a_id = b.a_id , a.is_active = 0 convert(varchar(10), a.cr_date, 111)   between @startdate , @enddate  select  '#d= '+cast(count(d.d_id) varchar)   d_count, '#c = '+cast(count(c.c_id) varchar)  c_count   #c c left join #d d    on c.c_id = d.c_id , c.is_active = 0 convert(varchar(10), c.cr_date, 111)   between @startdate , @enddate  dropping tables  drop table #a drop table #b drop table #c drop table #d 

i got following result of 2 select queries

result of 2 select queries mentioned in question

now, want merge both results of select queries, can't.

any 1 can tell me how can merge both queries single results in output

using union:

select  '#b= '+cast(count(b.b_id) varchar)   b_count, '#a = '+cast(count(a.a_id) varchar)  a_count   #a left join #b b    on a.a_id = b.a_id , a.is_active = 0 convert(varchar(10), a.cr_date, 111)   between @startdate , @enddate union select  '#d= '+cast(count(d.d_id) varchar)   d_count, '#c = '+cast(count(c.c_id) varchar)  c_count   #c c left join #d d    on c.c_id = d.c_id , c.is_active = 0 convert(varchar(10), c.cr_date, 111)   between @startdate , @enddate 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -