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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help With SQL Query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

markcr
Starting Member

1 Posts

Posted - 09/26/2012 :  12:58:49  Show Profile  Reply with Quote
Hi. I have a staff table and an assignments table. A staff member can have many assignments. I want to query the staff members based on their number of assignments for the month. The staff members should be ordered by the number of assignments from least to greatest. Here's what I'm starting with:

SELECT staff.staff_id, staff.first_name + ' ' + staff.last_name, COUNT( ...) this is where I'm not sure what to do? WHERE staff.staff_id = assignments.staff_id ORDER BY COUNT( ...again not sure...)

Thanks for any help you can provide. :-)

xhostx
Constraint Violating Yak Guru

USA
277 Posts

Posted - 09/26/2012 :  13:16:23  Show Profile  Reply with Quote
PLease show some sample of data to show us what your needs are.

the structure of the table will help too.



--------------------------
Joins are what RDBMS's do for a living
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 09/26/2012 :  13:20:11  Show Profile  Reply with Quote
Something like this?
SELECT
	s.staff_id,
	s.first_name + ' ' + s.last_name AS staff_name,
	COUNT(DISTINCT a.assignment_id) AS Assignments
FROM
	staff s
	LEFT JOIN assignments a ON
		a.staff_id = s.staff_id
GROUP BY
	s.staff_id,
	s.first_name + ' ' + s.last_name
ORDER BY
	Assignments;
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.06 seconds. Powered By: Snitz Forums 2000