Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. name varchar(20),
  2. age int
  3. )
  4.  
  5. INSERT INTO person VALUES
  6. ('temp',9),
  7. ('temp',10),
  8. ('temp',53),
  9. ('temp',23),
  10. ('temp',12),
  11. ('temp',42),
  12. ('temp',25),
  13. ('temp',28),
  14. ('temp',52),
  15. ('temp',60),
  16. ('temp',72),
  17. ('temp',61),
  18. ('temp',58),
  19. ('temp',73),
  20. ('temp',12),
  21. ('temp',42)
  22.  
  23. DECLARE @low int = 0
  24. DECLARE @t TABLE(ageRange varchar(10), amount int)
  25.  
  26. WHILE @low < (SELECT MAX(age) FROM person)
  27. BEGIN
  28. INSERT INTO @t
  29. SELECT CAST(@low AS CHAR(2))+' - '+CAST(@low+9 AS char(2)), COUNT(*)
  30. FROM person
  31. WHERE age BETWEEN @low AND @low+9
  32. SET @low = @low+10
  33. END
  34. SELECT * FROM @t WHERE amount > 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement