Как отобразить сумму значений в той же таблице в виде строки (в выписке)? - PullRequest
0 голосов
/ 06 сентября 2018

У меня есть запрос:

SELECT t.diapason, COUNT(distinct user_id) AS 'number_of_users'
    FROM (SELECT p.user_id, p.amount as total, CASE  
       when amount<=100 then '0-100' 
       when amount>100 and amount<=150 then '100-150' 
       when amount>150 then '>150 +' END AS diapason
       FROM 
         (SELECT payments.user_id, SUM(amount) AS amount 
         FROM payments INNER JOIN (SELECT DISTINCT user_id FROM activity 
          where login_time between '2018-04-12' and '2018-04-18') a ON 
          payments.user_id = a.user_id
          GROUP BY payments.user_id) p) t
GROUP BY t.diapason
ORDER BY number_of_users desc;

Что я должен изменить, чтобы отображать общее количество number_of_users в виде строки?

выход

+----------+-----------------+
| diapason | number_of_users |
+----------+-----------------+
| 0-100    |             278 |
| >150 +   |              16 |
| 100-150  |              12 |
| total    |             306 |    - I need this row
+----------+-----------------+

Спасибо!

Ответы [ 2 ]

0 голосов
/ 06 сентября 2018

Здесь уже есть хороший ответ, но в случае, если вы не можете использовать ROLLUP, вы можете использовать UNION:

SELECT t.diapason, COUNT(distinct user_id) AS 'number_of_users'
    FROM (SELECT p.user_id, p.amount as total, CASE  
       when amount<=100 then '0-100' 
       when amount>100 and amount<=150 then '100-150' 
       when amount>150 then '>150 +' END AS diapason
       FROM 
         (SELECT payments.user_id, SUM(amount) AS amount 
         FROM payments INNER JOIN (SELECT DISTINCT user_id FROM activity 
          where login_time between '2018-04-12' and '2018-04-18') a ON 
          payments.user_id = a.user_id
          GROUP BY payments.user_id) p) t
GROUP BY t.diapason
UNION ALL
SELECT 'total' AS diapason, COUNT(distinct user_id) AS 'number_of_users'
    FROM (SELECT p.user_id, p.amount as total, CASE  
       when amount<=100 then '0-100' 
       when amount>100 and amount<=150 then '100-150' 
       when amount>150 then '>150 +' END AS diapason
       FROM 
         (SELECT payments.user_id, SUM(amount) AS amount 
         FROM payments INNER JOIN (SELECT DISTINCT user_id FROM activity 
          where login_time between '2018-04-12' and '2018-04-18') a ON 
          payments.user_id = a.user_id
          GROUP BY payments.user_id) p) t
GROUP BY 1
ORDER BY number_of_users desc;

Надеюсь, это поможет

0 голосов
/ 06 сентября 2018

Взгляните на ROLLUP , что-то вроде:

SELECT IF(GROUPING(t.diapason), 'total', t.diapason) AS diapason,
       COUNT(distinct user_id) AS 'number_of_users'
FROM (SELECT p.user_id, p.amount as total, CASE  
   when amount<=100 then '0-100' 
   when amount>100 and amount<=150 then '100-150' 
   when amount>150 then '>150 +' END AS diapason
   FROM 
     (SELECT payments.user_id, SUM(amount) AS amount 
     FROM payments INNER JOIN (SELECT DISTINCT user_id FROM activity 
      where login_time between '2018-04-12' and '2018-04-18') a ON 
      payments.user_id = a.user_id
      GROUP BY payments.user_id) p) t
GROUP BY t.diapason WITH ROLLUP;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...