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
 Query about using count function in SQL
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

105ben
Starting Member

United Kingdom
16 Posts

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

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?

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

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

SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY

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

105ben
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  
 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.12 seconds. Powered By: Snitz Forums 2000