我正在尝试按条件语句的汇总进行分组,但是找不到正确的方法。
想到的一个想法是创建TEMPORARY TABLE并对其进行查询,但是在我们无法创建临时表的情况下,有什么想法吗?
当前查询:
select
case when count( p.id) = 1 then "Just One Photo"
when count(p.id) >1 and count(p.id) < 4 then "2 to 3 photos"
when count(p.id) >3 and count(p.id) < 6 then "4 to 5 photos"
when count(p.id) >5 then "More than 5 photos"
end bin
,count(p.id) as photo_count
,ra
,----re----
from table1 b
inner join table2 p on b.key = p.key
结果如下:
+----------------+-------------+-------+--------------+
| Aggregate | photo_count | ra | ----re---- |
+----------------+-------------+-------+--------------+
| Just One Photo | 1 | 4.0 | 166 |
| Just One Photo | 1 | 4.0 | 6 |
| Just One Photo | 1 | 2.0 | 5 |
| 2 to 3 photos | 3 | 2.0 | 10 |
| Just One Photo | 1 | 1.0 | 4 |
| Just One Photo | 1 | 2.0 | 6 |
| Just One Photo | 1 | 4.5 | 6 |
| 2 to 3 photos | 2 | 4.5 | 282 |
| Just One Photo | 1 | 4.0 | 91 |
| Just One Photo | 1 | 3.5 | 340 |
我希望的是
+----------------+-----------------+---------+--------------+
| Aggregate | avg(photo_coun) | avg(ra) | avg(re)--- |
+----------------+-----------------+---------+--------------+
| Just One Photo | 1.0 | 4.0 | 166 |
| 2 to 3 photos | 1.9 | 4.0 | 6 |