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 |
|
LOLCatLady
Starting Member
24 Posts |
Posted - 2012-02-10 : 10:51:01
|
I have this basic code that returns the number of orders by id within a specific timeframe. I need to limit the return data to all orders with a count of 4 or less. I don't want to see anybody with 5 or more orders. What do I need to add?SELECT id, COUNT(effdat) AS CountOrdereffdatFROM ordersWHERE (ordereffdat BETWEEN '03/01/2011' AND '02/29/2012') AND (ordertype = 'g') AND (id NOT LIKE 'C%')GROUP BY idORDER BY idSample returned data from above code:ID CountOrdereffdat00072 600093 600130 102360 302896 498765 10Thanks for helping!  |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-02-10 : 10:52:25
|
Use a HAVING clauseSELECT id, COUNT(effdat) AS CountOrdereffdatFROM ordersWHERE (ordereffdat BETWEEN '03/01/2011' AND '02/29/2012')AND (ordertype = 'g')AND (id NOT LIKE 'C%')GROUP BY idHAVING COUNT([effdat]) <= 4ORDER BY id Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
LOLCatLady
Starting Member
24 Posts |
Posted - 2012-02-10 : 10:54:55
|
That was quick and it works! Thanks so much!  quote: Originally posted by Transact Charlie Use a HAVING clauseSELECT id, COUNT(effdat) AS CountOrdereffdatFROM ordersWHERE (ordereffdat BETWEEN '03/01/2011' AND '02/29/2012')AND (ordertype = 'g')AND (id NOT LIKE 'C%')GROUP BY idHAVING COUNT([effdat]) <= 4ORDER BY id Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
|
 |
|
|
|
|
|