Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Distinct count from two tables 1

Status
Not open for further replies.

robinsql

Programmer
Aug 2, 2002
236
IE
Hi,

I am trying to get a distinct count of the phone numbers in the two tables, tblImportCalls and tblCalls. When I use the statement beneath it returns a count of distinct numbers which appear in both tblCalls AND tblImportCalls.
I need the count to reflect the fact that there might be numbers in tblImportCalls which are not in tblCalls and vice versa. It does not count the numbers which appear in only one of the tables.

Can somebody show me what's wrong with the statement...

SELECT COUNT(DISTINCT tblCalls.PhoneNumber)
FROM tblCalls join tblImportCalls
on tblCalls.PhoneNumber = tblImportCalls.PhoneNumber
WHERE MONTH(tblCalls.DateOfCall) = 6
AND YEAR(tblCalls.DateOfCall) = 2003


Thanks for any help,

Robin
 
SELECT tblCalls.PhoneNumber,COUNT(*) as Num
FROM tblCalls join tblImportCalls
on tblCalls.PhoneNumber = tblImportCalls.PhoneNumber
WHERE MONTH(tblCalls.DateOfCall) = 6
AND YEAR(tblCalls.DateOfCall) = 2003
group by tblCalls.PhoneNumber
order by tblCalls.PhoneNumber
 
Hi,
When I try this I receive the following...

Arithmetic overflow error converting expression to data type int.
 
Code:
SELECT COUNT(DISTINCT PhoneNumber), 'tblCalls'
FROM tblCalls 
WHERE MONTH(tblCalls.DateOfCall) = 6
AND YEAR(tblCalls.DateOfCall) = 2003
union all
select count(distinct phoneNumber), 'tblImportCalls'
from tblImportCalls
 
Hi,
This returns two rows of data...
182 tblCalls
191 tblImportCalls

Basically I need one figure to be returned as I am using this within a Stored Proc.
I'm probably not explaining myself too well so in it's simplest terms...
If I have a table with the Numbers 1, 2, 3, 4, 5 and another with the numbers 4, 5, 6, 7, 8. I need the value returned to be 8 as there are 8 distinct numbers in the two tables.

Thanks for any help,

Robin
 
Code:
SELECT COUNT(DISTINCT PhoneNumber)
from (select PhoneNumber 
FROM tblCalls
WHERE MONTH(tblCalls.DateOfCall) = 6
AND YEAR(tblCalls.DateOfCall) = 2003
union all
select phoneNumber
from tblImportCalls) dt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top