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.
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 countFROM resource_line having count(resource_id) < 3group by resource_idThe 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 3can anyone help? |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-12-27 : 10:15:14
|
The order of keywords is:SELECTFROMWHEREGROUP BYHAVINGORDER BYSo 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. |
|
|
105ben
Starting Member
16 Posts |
Posted - 2012-12-27 : 10:17:47
|
Worked this time, swapped them around as you said!Brilliant, thanks! |
|
|
|
|
|