当前位置: 代码迷 >> SQL >> 用一条sql话语显示数据百分比并加百分号
  详细解决方案

用一条sql话语显示数据百分比并加百分号

热度:71   发布时间:2016-05-05 12:02:04.0
用一条sql语句显示数据百分比并加百分号

求数值所占比重

关键点:(round(t1.cnt/t2.totalCount*100,2))||'%'

?

例子:

如下表所示,车辆信息注册表carInfo

IDCAR_TYPECAR_ONWERCAR_PRICEREG_TIME
0001雪佛兰通用张三1002013-09-11
0002雪佛兰通用李四1502013-09-13
0003别克君威王五702013-09-11
0004凯悦赵六502013-09-11
0005江淮孙七90

2013-09-13

0006江淮丁8902013-09-14

?要求用一条sql语句输出下列格式(按日期分类,百分比保留小数点后两位)

注册时间车数所占总车数百分比当日总价所占全部总价百分比
2013-09-14116.67%9016.36%
2013-09-13233.33%24043.64%
2013-09-11350%22040%

?

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

?

?

?

?

  相关解决方案