Guest User

Untitled

a guest
Feb 21st, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. CREATE TABLE checkins
  2. (
  3. date_in DateTime,
  4. date_out DateTime,
  5. age Int32,
  6. sex String,
  7. date_day Date MATERIALIZED toDate(date_in)
  8. ) ENGINE = MergeTree(date_day, date_in, 8192)
  9.  
  10. INSERT INTO checkins VALUES
  11. (toDateTime('2018-01-01 08:30:00'), toDateTime('2018-01-01 16:30:00'), 32, 'M'),
  12. (toDateTime('2018-01-01 09:30:00'), toDateTime('2018-01-01 10:30:00'), 28, 'M'),
  13. (toDateTime('2018-01-01 10:15:00'), toDateTime('2018-01-01 10:45:00'), 30, 'M'),
  14. (toDateTime('2018-01-01 11:30:00'), toDateTime('2018-01-01 11:45:00'), 35, 'M'),
  15. (toDateTime('2018-01-01 14:30:00'), toDateTime('2018-01-01 17:30:00'), 25, 'F');
  16.  
  17. SELECT count(*), instant
  18. FROM
  19. (
  20. SELECT arrayJoin([toDateTime('2018-01-01 10:00:00'), toDateTime('2018-01-01 12:00:00'), toDateTime('2018-01-01 14:00:00'), toDateTime('2018-01-01 16:00:00')] AS tabinstants) AS instant
  21. FROM checkins
  22. WHERE (date_in < instant) AND (date_out > instant)
  23. )
  24. GROUP BY instant
  25. ORDER BY instant ASC
  26.  
  27. ┌─count()─┬─────────────instant─┐
  28. │ 2 │ 2018-01-01 10:00:00 │
  29. │ 1 │ 2018-01-01 12:00:00 │
  30. │ 1 │ 2018-01-01 14:00:00 │
  31. │ 2 │ 2018-01-01 16:00:00 │
  32. └─────────┴─────────────────────┘
  33.  
  34. ┌─count()─┬─────────────instant─┐
  35. │ 3 │ 2018-01-01 10:00:00 │
  36. │ 1 │ 2018-01-01 12:00:00 │
  37. │ 2 │ 2018-01-01 14:00:00 │
  38. │ 2 │ 2018-01-01 16:00:00 │
  39. └─────────┴─────────────────────┘
Add Comment
Please, Sign In to add comment