- 客分类:
- oracle技术
求数值所占比重
关键点:(round(t1.cnt/t2.totalCount*100,2))||'%'
例子:
如下表所示,车辆信息注册表carInfo
ID | CAR_TYPE | CAR_ONWER | CAR_PRICE | REG_TIME |
0001 | 雪佛兰通用 | 张三 | 100 | 2013-09-11 |
0002 | 雪佛兰通用 | 李四 | 150 | 2013-09-13 |
0003 | 别克君威 | 王五 | 70 | 2013-09-11 |
0004 | 凯悦 | 赵六 | 50 | 2013-09-11 |
0005 | 江淮 | 孙七 | 90 | 2013-09-13 |
0006 | 江淮 | 丁8 | 90 | 2013-09-14 |
要求用一条sql语句输出下列格式(按日期分类,百分比保留小数点后两位)
注册时间 | 车数 | 所占总车数百分比 | 当日总价 | 所占全部总价百分比 |
2013-09-14 | 1 | 16.67% | 90 | 16.36% |
2013-09-13 | 2 | 33.33% | 240 | 43.64% |
2013-09-11 | 3 | 50% | 220 | 40% |
sql语句如下:
select t1.reg_time 注册时间,t1.cnt 车数,(round(t1.cnt/t2.totalCount*100,2))||'%' 所占总量百分比,
t1.car_price 当日总价,(round(t1.car_price/t3.totalPrice*100,2))||'%' 所占全部总价百分比
from (select reg_time,count(*) cnt,sum(car_price) car_price from carInfo group by reg_time order by reg_time desc) t1,
(select count(*) totalCount from carInfo) t2,
(select sum(car_price) totalPrice from carInfo) t3