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.
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:
In
previous articles I explained SQL Server change identity column value, replace multiple spaces with single
space in sql,
reseed identity column value in sql
server,
get only month and year from date in
sql server,
substring function in SQL server
and many articles relating to SQL
server.
Now I will explain how to get total records count of union query in sql
server 2008.
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. |
|||
|
|||
3 comments :
Nice Query
I tried this query in mysql but its not working.
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???
Note: Only a member of this blog may post a comment.