szymski

Untitled

May 24th, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. --explain
  2. with summary as (
  3. select
  4. count(*) over() as total_all,
  5. m.id,
  6. m."externalId",
  7. m."from",
  8. m.to,
  9. m.title,
  10. m."dateReceived",
  11. m.removed,
  12. -- row_number() over (partition by m.title order by m."dateReceived" desc) / 10 as super_id,
  13. row_number() over w_date as rank
  14. -- count(*) over w_no_date as count,
  15. -- sum(cast(m.removed as int)) over w_no_date as "totalRemoved",
  16. -- count(*) over() as full_count
  17. from public."Mail" m
  18. -- having rank = 1
  19. window
  20. w_date as (partition by m.title order by m."dateReceived" desc),
  21. w_no_date as (partition by m.title)
  22. order by m."dateReceived" desc
  23. )
  24. select
  25. count(*) over() as total_grouped,
  26. *,
  27. row_number() over(partition by s.title, (s.rank - 1) / 10 order by s."dateReceived" desc, s.rank) as rank2,
  28. count(*) over(partition by s.title, (s.rank - 1) / 10) as count
  29. -- COUNT(*) over() as total_grouped
  30. from summary s
  31. --where rank = 1
  32. --where ((s.rank - 1) % 10) = 0
  33. --order by s.title, s."dateReceived" desc
  34. order by s."dateReceived" desc, s.rank
  35. limit 10000 offset 0
Advertisement
Add Comment
Please, Sign In to add comment