这是我的查询:
select
t.range as [score range], count(*) as [number of occurences]
from
(select
case
when answer_count between 0 and 5 then ' 0- 9'
when answer_count between 5 and 10 then '10-19'
else '20-99' end as range
from
points
where
type = 'product_quiz') t
group by
t.range
输出为:
/-------------------------------------\
| score range | number of occurrences |
|-------------+-----------------------|
| 10-19 | 121327 |
| 0- 9 | 129195 |
\-------------------------------------/
但是我想要这样的输出:
/-----------------------------------------------------------------------------\
| score_range | number_of_occurrences | score_range1 | number_of_occurrences1 |
|-------------+-----------------------+--------------+------------------------|
| 10-19 | 121327 | 10-19 | 121327 |
| 0- 9 | 129195 | 0- 9 | 129195 |
\-----------------------------------------------------------------------------/
如何使用子查询来实现?
如果我对您的理解正确,则不需要子查询(除非您引用的是子句中已存在的子查询FROM
):
select
t.range as [score_range],
count(*) as [number_of_occurences],
t.range as [score_range1],
count(*) as [number_of_occurences1]
from
(select
case
when answer_count between 0 and 5 then ' 0- 9'
when answer_count between 5 and 10 then '10-19'
else '20-99' end as range
from
points
where
type = 'product_quiz') t
group by
t.range
为什么要执行此操作(以及为什么特别要使用子查询)是另一个问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句