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
 General SQL Server Forums
 New to SQL Server Programming
 Query about using count function in SQL

Author  Topic 

105ben
Starting Member

16 Posts

Posted - 2012-12-27 : 10:06:40
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
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-12-27 : 10:15:14
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

16 Posts

Posted - 2012-12-27 : 10:17:47
Worked this time, swapped them around as you said!

Brilliant, thanks!
Go to Top of Page
   

- Advertisement -