Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Count the total from 2 queries

Author  Topic 

wewe_2090
Starting Member

4 Posts

Posted - 2014-04-09 : 00:35:20
dear all,

how to count the total for 2 queries from 2 different tables.

query 1:
select COUNT (SEGMENT_CODE) AS TOTALROW FROM table1 WHERE SEGMENT_CODE = 'FF'


i get the total : 2

query 2:
select COUNT (SEGMENT_CODE) AS TOTALROW FROM table2 WHERE SEGMENT_CODE = 'FF'


i get the total : 3

how to combine 2 query then to get the total 2 + 3 = 5

please help.
thanks.

stepson
Aged Yak Warrior

545 Posts

Posted - 2014-04-09 : 01:13:20
there are a few ways:

select sum(TOTALROW) as TOTALROW

FROM

( select COUNT (SEGMENT_CODE) AS TOTALROW FROM table1 WHERE SEGMENT_CODE = 'FF'
UNION ALL
select COUNT (SEGMENT_CODE) AS TOTALROW FROM table2 WHERE SEGMENT_CODE = 'FF')A





sabinWeb MCP
Go to Top of Page

stepson
Aged Yak Warrior

545 Posts

Posted - 2014-04-09 : 01:14:48
[code]
declare @TR1 as int,@TR2 as int


select @TR1=COUNT (SEGMENT_CODE) FROM table1 WHERE SEGMENT_CODE = 'FF'

select @TR2=COUNT (SEGMENT_CODE) FROM table2 WHERE SEGMENT_CODE = 'FF')A


select (@TR1+@TR2) as TOTALROW
[/code]


sabinWeb MCP
Go to Top of Page

wewe_2090
Starting Member

4 Posts

Posted - 2014-04-09 : 05:51:51
quote:
Originally posted by stepson

there are a few ways:

select sum(TOTALROW) as TOTALROW

FROM

( select COUNT (SEGMENT_CODE) AS TOTALROW FROM table1 WHERE SEGMENT_CODE = 'FF'
UNION ALL
select COUNT (SEGMENT_CODE) AS TOTALROW FROM table2 WHERE SEGMENT_CODE = 'FF')A



thank you it solved.
really appreciated.

sabinWeb MCP

Go to Top of Page
   

- Advertisement -