Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

SQL Server - Get Total Records Count in Union Query

Jul 27, 2014
Introduction:

Here I will explain how to get total records count of union query in
sql server 2008 or sql server query to get total records of union statement with example.
Description:

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

 

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

3 comments :

sanju said...

Nice Query

थोडंसं मनातलं.... said...

I tried this query in mysql but its not working.

Subhajit Samanta said...

hi suresh,
in a table have population for male and female according to city wise.
please tell me how i find total population of each city???

Give your Valuable Comments

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

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.