HI WELCOME TO KANSIRIS

Get Total Records Count in Union Query in SQL server

Leave a Comment
To get total count of records in union query in sql server that syntax will be like as shown below

Syntax to get total records count of union query


select count(1) from
(
select id from @table1
union
select id from @table2
)as totalrecords
Example:


declare @table1 table(id int,name varchar(50),education varchar(50))

declare @table2 table(id int,name varchar(50),education varchar(50))


insert into @table1(id,name,education)
values(1,'suresh','b.tech')
insert into @table1(id,name,education)
values(2,'rohini','msc')
insert into @table1(id,name,education)
values(3,'praveen','btech')

insert into @table2(id,name,education)
values(1,'sateesh','md')
insert into @table2(id,name,education)
values(2,'madhav','mba')
insert into @table2(id,name,education)
values(3,'Mahendra','ca')

select count(1) from (
select id,name,education from @table1
union
select id,name,education from @table2
) as result
When we run above query it will return total records count 6 like as shown below

Output

 

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.