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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Query to match 2 data sets
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

alastairc
Starting Member

1 Posts

Posted - 09/30/2012 :  20:14:47  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 09/30/2012 :  21:45:56  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000