如何将同一表中的总值显示为一行(在case语句中)?

Posted

技术标签:

【中文标题】如何将同一表中的总值显示为一行(在case语句中)?【英文标题】:How to display total of values in the same table as a row (in case statement)? 【发布时间】:2019-02-11 07:45:58 【问题描述】:

我有一个问题:

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
+----------+-----------------+

谢谢!

【问题讨论】:

你的样本数据是什么? 你可以联合 案例表达式。 (返回一个值。) 我可以在dba.stackexchange.com/questions/68791/group-by-in-ranges找到更好的处理sql范围的方法 【参考方案1】:

看看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;

【讨论】:

谢谢!有用。但它显示:NULL | 306. 如何更改“total”而不是“NULL”? @ЕлисейГорьков 更新了我的答案,请查看。如果它对您有帮助,请不要忘记接受它作为正确答案。快乐编码! :) ERROR 1221 (HY000):CUBE/ROLLUP 和 ORDER BY 的使用不正确 谢谢,但是...错误 1305 (42000):FUNCTION businessanalysis.GROUPING 不存在 GROUPING 功能是 mysql 8.0 中的新功能。见mysqlserverteam.com/mysql-8-0-grouping-function【参考方案2】:

这里已经有一个很好的答案,但万一你不能使用 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;

希望对你有帮助

【讨论】:

以上是关于如何将同一表中的总值显示为一行(在case语句中)?的主要内容,如果未能解决你的问题,请参考以下文章

使用 case 语句根据不同表中的数据显示数据

sql 将多行显示为一行,如果一张表中有几行数据中的同一列的值是相同,那么只显示为一行的数据

如何根据case语句在连接多个表时选择一行

如何使用 UITableview 或 UICollectionView 在表格的最后一行显示数字列的总值?

如何使用 SQL 中的 Case 语句将数据插入临时表

如何在一个表中显示多行,另一表中的一行显示为单行