Sql 在查询中使用汇总总数
表1.费用:Sql 在查询中使用汇总总数,sql,oracle,oracle9i,Sql,Oracle,Oracle9i,表1.费用: Item | Cost Car | 10 Cat | 2 Dog | 3 Fish| 1 是否可以在每行查询中使用总和 SELECT Item, Cost, Cost/sum(cost) "Percent" from Costs where ... 要获得: Item | Cost | Percent Car | 10 | 0.625 Cat | 2 | 0.125 Dog | 3 | 0.1875 Fish| 1 | 0.0
Item | Cost
Car | 10
Cat | 2
Dog | 3
Fish| 1
是否可以在每行查询中使用总和
SELECT Item, Cost, Cost/sum(cost) "Percent" from Costs where ...
要获得:
Item | Cost | Percent
Car | 10 | 0.625
Cat | 2 | 0.125
Dog | 3 | 0.1875
Fish| 1 | 0.0625
这只是一个简单的例子。我的查询要复杂得多,所以我不想再做一次完整的表扫描来获得“selectsum(cost)where…”。是否可以使用分组/汇总进行此操作?您可以使用函数获取比率,而无需计算总和
查询1:
select item,
cost,
ratio_to_report(cost) over () as percent
from costs
| ITEM | COST | PERCENT |
|------|------|---------|
| car | 10 | 0.625 |
| cat | 2 | 0.125 |
| dog | 3 | 0.1875 |
| fish | 1 | 0.0625 |
:
select item,
cost,
ratio_to_report(cost) over () as percent
from costs
| ITEM | COST | PERCENT |
|------|------|---------|
| car | 10 | 0.625 |
| cat | 2 | 0.125 |
| dog | 3 | 0.1875 |
| fish | 1 | 0.0625 |
假设成本表有一百万行,复杂的查询只过滤出结果集的10行。你需要10行还是100万行的总和?