Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SELECT t.userInn, t.first_part as first_part, t.second_part as second_part
- FROM (
- SELECT t.userInn, IF(t.first_cnt > 0, ROUND(t.first_sum / t.first_cnt), 0) as first_part,
- IF(t.second_sum > 0, ROUND(t.second_sum / t.second_cnt), 0) as second_part
- FROM (
- SELECT t.userInn, SUM(t.totalSum * t.second_part) as second_sum, SUM(t.second_part) as second_cnt,
- SUM(t.totalSum * (1 - t.second_part)) as first_sum, SUM(1 - t.second_part) as first_cnt
- FROM (
- SELECT userInn, INT(Hour(timeDate) >= 13) as second_part, totalSum
- FROM kkt_document_json
- WHERE subtype == 'receipt'
- ) as t
- GROUP BY t.userInn
- ) as t
- ) as t
- WHERE first_part > second_part
- ORDER BY first_part, t.userInn
- LIMIT 50;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement