Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. SELECT t.userInn, t.first_part as first_part, t.second_part as second_part
  2. FROM (
  3. SELECT t.userInn, IF(t.first_cnt > 0, ROUND(t.first_sum / t.first_cnt), 0) as first_part,
  4. IF(t.second_sum > 0, ROUND(t.second_sum / t.second_cnt), 0) as second_part
  5. FROM (
  6. SELECT t.userInn, SUM(t.totalSum * t.second_part) as second_sum, SUM(t.second_part) as second_cnt,
  7. SUM(t.totalSum * (1 - t.second_part)) as first_sum, SUM(1 - t.second_part) as first_cnt
  8. FROM (
  9. SELECT userInn, INT(Hour(timeDate) >= 13) as second_part, totalSum
  10. FROM kkt_document_json
  11. WHERE subtype == 'receipt'
  12. ) as t
  13. GROUP BY t.userInn
  14. ) as t
  15. ) as t
  16. WHERE first_part > second_part
  17. ORDER BY first_part, t.userInn
  18. LIMIT 50;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement