我正在运行此查询并获取下面附带的数据
mysql> ( SELECT date_range AS 'Time Elapsed', COUNT(*) AS 'Conversions' FROM `data` GROUP BY date_sort_sno, `date_range` ORDER BY date_sort_sno )
-> UNION ALL
-> ( SELECT date_range AS 'Time Elapsed', COUNT(*) AS 'A_Conversio' FROM `data` GROUP BY date_sort_sno, `date_range` WHERE name = 'Alpha' ORDER BY date_sort_sno );
输出
+------------------------------+-------------+
| Time Elapsed | Conversions |
+------------------------------+-------------+
| Less than equal to 5 minutes | 1391 |
| 5 to 30 minutes | 9108 |
| 30 to 60 minutes | 2233 |
| 1 hour to 3 hours | 2280 |
| 3 hours to 24 hours | 4585 |
| 1 to 3 days | 4143 |
| 3 to 6 days | 155 |
| more than 6 days | 80 |
| Less than equal to 5 minutes | 1391 |
| 5 to 30 minutes | 9108 |
| 30 to 60 minutes | 2233 |
| 1 hour to 3 hours | 2280 |
| 3 hours to 24 hours | 4585 |
| 1 to 3 days | 4143 |
| 3 to 6 days | 155 |
| more than 6 days | 80 |
+------------------------------+-------------+
问题陈述: 我想根据经过的时间合并数据
+------------------------------+-------------+-------------+
| Time Elapsed | Conversions | A_Conversio |
+------------------------------+-------------+-------------+
| Less than equal to 5 minutes | 1391 | 1231 |
| 5 to 30 minutes | 1391 | 4455 |
| 30 to 60 minutes | 2233 | 3333 |
| 1 hour to 3 hours | 2280 | 4343 |
| 3 hours to 24 hours | 4585 | 2234 |
| 1 to 3 days | 4143 | 2344 |
| 3 to 6 days | 155 | 455 |
| more than 6 days | 80 | 11 |
+------------------------------+-------------+-------------+
答案 0 :(得分:1)
而不是两个单独的查询,您可以有一个包含两列的查询 - 一个普通的count(*)
来计算所有记录,一个count
函数应用于case
表达式来计算仅限名称为'Alpha'
的转化:
SELECT date_range AS 'Time Elapsed',
COUNT(*) AS 'Conversions',
COUNT(CASE name WHEN 'Alpha' THEN 1 END) AS 'A_Conversio'
FROM `data`
GROUP BY date_sort_sno, `date_range`
ORDER BY date_sort_sno