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)
 Query to match 2 data sets

Author  Topic 

alastairc
Starting Member

1 Post

Posted - 2012-09-30 : 20:14:47
Hi team,

This query has me stumped.It should be easy, but i can't get it. The null line (ie when there is no match) is the tough bit. Forgive me for my simple query!

Table 1: Appointments
TYPE
Regular
Regular
Regular
Merch


Table 2: Plans
TYPE
Regular
Merch
Special


Result Needed
TYPE COUNT
Regular 3
Merch 1
Special 0

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-30 : 21:45:56
Use a LEFT OUTER JOIN, like shown below:
SELECT
a.[Type],
COUNT(b.[type]) AS [Count]
FROM
Plans a
LEFT JOIN Appointments b ON a.[type] = b.[type]
GROUP BY
a.[type]
Go to Top of Page
   

- Advertisement -