1. 程式人生 > >sql sql計算除法保留兩位小數sql中分母為零處理

sql sql計算除法保留兩位小數sql中分母為零處理

1

 select  
d.name,
case when SUM(a.grid_capacity)=0 then 0 else ROUND(SUM(c.year_power)/SUM(a.grid_capacity), 2)   end as 'hours', 
SUM(c.year_power)as '年總量',
SUM(a.grid_capacity) as '裝機'
 from ct_project_build_info_grid_accept a 
left JOIN ct_project_info b on a.project_no=b.project_no
left JOIN t_power_station_year c on a.project_no=c.project_no
LEFT JOIN city d on b.city_area_code=d.area_code
where b.type=1 and c.`year`=2018 
GROUP BY b.city_area_code

 

2

select a, b, a/b from tabName
當b=0時出錯,解決方法:
select a, b, (case when b=0 then 0 else a/b end) from tabName