SELECT
SUM(CASE WHEN age >= 80 THEN 1 ELSE 0 END) AS '>=80',
SUM(CASE WHEN age BETWEEN 70 AND 79 THEN 1 ELSE 0 END) AS '70-79',
SUM(CASE WHEN age BETWEEN 60 AND 69 THEN 1 ELSE 0 END) AS '60-69',
SUM(CASE WHEN age BETWEEN 50 AND 59 THEN 1 ELSE 0 END) AS '50-59',
SUM(CASE WHEN age BETWEEN 40 AND 49 THEN 1 ELSE 0 END) AS '40-49',
SUM(CASE WHEN age BETWEEN 30 AND 39 THEN 1 ELSE 0 END) AS '30-39',
SUM(CASE WHEN age BETWEEN 20 AND 30 THEN 1 ELSE 0 END) AS '20-29',
SUM(CASE WHEN age BETWEEN 10 AND 19 THEN 1 ELSE 0 END) AS '10-19',
SUM(CASE WHEN age BETWEEN 0 AND 9 THEN 1 ELSE 0 END) AS '0-9'
FROM (SELECT * FROM table) a
我正在使用上面的查询来存储年龄范围,并且输出:
+------+-------+-------+-------+-------+-------+--------+---------+---------+
| >=80 | 70-79 | 60-69 | 50-59 | 40-49 | 30-39 | 20-29 | 10-19 | 0-9 |
+------+-------+-------+-------+-------+-------+--------+---------+---------+
| 136 | 394 | 1273 | 2530 | 3298 | 15384 | 194099 | 2244405 | 9780789 |
+------+-------+-------+-------+-------+-------+--------+---------+---------+
我需要将其转换为列格式,或者找到一种替代的存储方法,以允许上述查询生成与值行相反的列。
期望的输出:
+-----------+----------*
| age_range | freq |
+-----------+----------*
| >=80 | 136 |
+-----------+----------*
| 70-79 | 394 |
+-----------+----------*
| 60-69 | 1273 |
+-----------+----------*
| 50-59 | 2530 |
+-----------+----------*
| 40-49 | 3298 |
+-----------+----------*
| 30-39 | 15384 |
+-----------+----------*
| 20-29 | 194099 |
+-----------+----------*
| 10-19 | 2244405 |
+-----------+----------*
| 0-9 | 9780789 |
+-----------+----------*
据我所知,Impala不支持数据透视?
感谢您的帮助,谢谢
case
键使用group by
表达式:
SELECT (CASE WHEN age >= 80 THEN '>=80',
WHEN age BETWEEN 70 AND 79 THEN '70-79',
WHEN age BETWEEN 60 AND 69 THEN '60-69',
WHEN age BETWEEN 50 AND 59 THEN '50-59',
WHEN age BETWEEN 40 AND 49 THEN '40-49',
WHEN age BETWEEN 30 AND 39 THEN '30-39',
WHEN age BETWEEN 20 AND 30 THEN '20-29',
WHEN age BETWEEN 10 AND 19 THEN'10-19',
WHEN age BETWEEN 0 AND 9 THEN '0-9'
END) as age_group,
COUNT(*)
FROM a
GROUP BY age_group;