Please start any new threads on our new site at 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
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query about using count function in SQL
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

United Kingdom
16 Posts

Posted - 12/27/2012 :  10:06:40  Show Profile  Reply with Quote

I need to create some code that will return from this table every resource_id that appears in the table less than three time:

resource_line (line_id, resource_id)

Here's the code I have:

SELECT resource_id, count(resource_id) as count
FROM resource_line
having count(resource_id) < 3
group by resource_id

The line is red is the one that is causing the issues!

i can get it to return the resource_id's and count's without that red line in. I just dont know how to specify only records with count less than 3

can anyone help?

Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/27/2012 :  10:15:14  Show Profile  Reply with Quote
The order of keywords is:


So GROUP BY needs to be before the HAVING clause.

May aid to memory for this order of keywords is that if it were not for GROUP BY interrupting the party, WHERE, HAVING and ORDER BY would spell the word WHO.
Go to Top of Page

Starting Member

United Kingdom
16 Posts

Posted - 12/27/2012 :  10:17:47  Show Profile  Reply with Quote
Worked this time, swapped them around as you said!

Brilliant, thanks!
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