code_junkie

SQL Query Help: Selecting Rows That Appear A Certain Number Of Times

Nov 11th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. select dates
  2. from table t
  3. group by dates having count(dates) < k ;
  4.  
  5. SELECT * FROM [MyTable] WHERE [Date] IN
  6. (
  7. SELECT [Date]
  8. FROM [MyTable]
  9. GROUP By [Date]
  10. HAVING COUNT(*) < @Max
  11. )
  12.  
  13. SELECT Date
  14. FROM SomeTable
  15. GROUP BY Date
  16. HAVING COUNT(*) < @k
  17.  
  18. DECLARE @Max int
  19. SELECT @Max = 5
  20.  
  21. SELECT t1.*
  22. FROM [MyTable] t1
  23. JOIN(
  24. SELECT [Date]
  25. FROM [MyTable]
  26. GROUP By [Date]
  27. HAVING COUNT(*) < @Max
  28. ) t2 on t1.[Date] = t2.[Date]
  29.  
  30. SELECT Date FROM table GROUP BY Date HAVING COUNT(*)<k
  31.  
  32. SELECT * FROM Table WHERE Date IN (
  33. SELECT Date FROM table GROUP BY Date HAVING COUNT(*)<k)
  34.  
  35. SELECT date, COUNT(date)
  36. FROM table
  37. GROUP BY date
  38. HAVING COUNT(date) < k
  39.  
  40. SELECT table.*
  41. FROM table
  42. INNER JOIN (
  43. SELECT date, COUNT(date)
  44. FROM table
  45. GROUP BY date
  46. HAVING COUNT(date) < k) dates ON table.date = dates.date
  47.  
  48. select date_col,count(*)
  49. from your_table
  50. group by date_col
  51. having count(*) < 5;
  52.  
  53. select trunc(date_col) as date_col,count(*)
  54. from your_table
  55. group by trunc(date_col)
  56. having count(*) < 5;
Add Comment
Please, Sign In to add comment