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
 General SQL Server Forums
 New to SQL Server Programming
 Strange request

Author  Topic 

godspeedba
Yak Posting Veteran

90 Posts

Posted - 2010-03-12 : 08:29:50
I have two tables which contain address'.

I have been asked to find the total occurence of each town for both databases.

I was thinking that I would have to use two temptables to find the town_name and Count(town_name) and then somehow merge the two temptables and the totals into one.

What is the best way to approach this?

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-03-12 : 08:38:19
This?
select town, count(*) as towncount
from
(
select town from DB1.dbo.TownTable
UNION ALL
select town from DB2.dbo.TownTable
)dt
group by town
order by town


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

godspeedba
Yak Posting Veteran

90 Posts

Posted - 2010-03-12 : 08:41:28
Never thought of attempting it in that way, I'll give it a try, thanks webfred
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-03-12 : 12:24:10
Group By state too, else your counts will be wrong.

For example, this returns 26 rows in one of my databases:

select distinct state from customers where city = 'springfield'
Go to Top of Page
   

- Advertisement -