执行数学运算以从 COUNT 和 GROUP BY 语句输出
Do math operations to output from COUNT and GROUP BY statemetns
假设我有一个 Yes/No 数据列。然后我可以通过
计算每个结果类别中的观察结果
Select MyVar, count(MyVar) from MyTable
group by MyVaR;
输出类似于:
MyVar count(MyVar)
Yes 10
No 5
如何向此输出添加另一个元素table,这是分数 (Yes/No) 所以我的最终 table 类似于:
MyVar count(MyVar)
Yes 10
No 5
Fraction 2
有没有简单的解决方法?
这是最简单的 -- 在这种格式下 -- 使用 union all
:
with t as (
Select MyVar, count(MyVar) as cnt
from MyTable
group by MyVaR
)
select MyVar
from t
union all
select 'Ratio', sum(case when MyVar = 'Yes' then cnt else 0 end) / sum(case when MyVar = 'No' then cnt else 0 end)
from t;
也就是说,我会使用条件聚合并将所有值放在一行中:
select sum(case when MyVar = 'Yes' then cnt else 0 end) as yes,
sum(case when MyVar = 'Yes' then cnt else 0 end) as no,
sum(case when MyVar = 'Yes' then cnt else 0 end) / sum(case when MyVar = 'No' then cnt else 0 end) as ratio
from t;
你可以使用 group by grouping sets()
:
Select
case
when grouping(MyVar)=0 then MyVar
else 'Fraction'
end grouping_value,
case
when grouping(MyVar)=0 then count(MyVar)
else count(decode(MyVar,'Yes',1)) / count(decode(MyVar,'No',1))
end c
from MyTable
group by grouping sets(MyVaR,())
带有示例数据的完整测试用例:
with MyTable(MyVar) as (
select *
from table(sys.odcivarchar2list(
'Yes','Yes','Yes','Yes','Yes','Yes','Yes','Yes','Yes','Yes',
'No','No','No','No','No'
))
)
Select
case
when grouping(MyVar)=0 then MyVar
else 'Fraction'
end grouping_value,
case
when grouping(MyVar)=0 then count(MyVar)
else count(decode(MyVar,'Yes',1)) / count(decode(MyVar,'No',1))
end val
from MyTable
group by grouping sets(MyVaR,())
;
结果:
GROUPING_VALUE VAL
--------------- ----------
No 5
Yes 10
Fraction 2
假设我有一个 Yes/No 数据列。然后我可以通过
计算每个结果类别中的观察结果Select MyVar, count(MyVar) from MyTable
group by MyVaR;
输出类似于:
MyVar count(MyVar)
Yes 10
No 5
如何向此输出添加另一个元素table,这是分数 (Yes/No) 所以我的最终 table 类似于:
MyVar count(MyVar)
Yes 10
No 5
Fraction 2
有没有简单的解决方法?
这是最简单的 -- 在这种格式下 -- 使用 union all
:
with t as (
Select MyVar, count(MyVar) as cnt
from MyTable
group by MyVaR
)
select MyVar
from t
union all
select 'Ratio', sum(case when MyVar = 'Yes' then cnt else 0 end) / sum(case when MyVar = 'No' then cnt else 0 end)
from t;
也就是说,我会使用条件聚合并将所有值放在一行中:
select sum(case when MyVar = 'Yes' then cnt else 0 end) as yes,
sum(case when MyVar = 'Yes' then cnt else 0 end) as no,
sum(case when MyVar = 'Yes' then cnt else 0 end) / sum(case when MyVar = 'No' then cnt else 0 end) as ratio
from t;
你可以使用 group by grouping sets()
:
Select
case
when grouping(MyVar)=0 then MyVar
else 'Fraction'
end grouping_value,
case
when grouping(MyVar)=0 then count(MyVar)
else count(decode(MyVar,'Yes',1)) / count(decode(MyVar,'No',1))
end c
from MyTable
group by grouping sets(MyVaR,())
带有示例数据的完整测试用例:
with MyTable(MyVar) as (
select *
from table(sys.odcivarchar2list(
'Yes','Yes','Yes','Yes','Yes','Yes','Yes','Yes','Yes','Yes',
'No','No','No','No','No'
))
)
Select
case
when grouping(MyVar)=0 then MyVar
else 'Fraction'
end grouping_value,
case
when grouping(MyVar)=0 then count(MyVar)
else count(decode(MyVar,'Yes',1)) / count(decode(MyVar,'No',1))
end val
from MyTable
group by grouping sets(MyVaR,())
;
结果:
GROUPING_VALUE VAL
--------------- ----------
No 5
Yes 10
Fraction 2