sql纵向,横向,去Null值,去0值求平均数
SELECT
round( avg( nullif( temperature, 0 )), 2 ) temperature,
sum( electricity_sum + electricity_1 + electricity_2 + electricity_3 ) AS electricity_sum,
sum( power_sum ) AS power_sum,
round((
IF
(
avg(
nullif( voltage_sum, 0 )) IS NULL,
0,
avg(
nullif( voltage_sum, 0 )))+
IF
(
avg(
nullif( voltage_1, 0 )) IS NULL,
0,
avg(
nullif( voltage_1, 0 )))+
IF
(
avg(
nullif( voltage_2, 0 )) IS NULL,
0,
avg(
nullif( voltage_2, 0 )))+
IF
(
avg(
nullif( voltage_3, 0 )) IS NULL,
0,
avg(
nullif( voltage_3, 0 ))))/(
IF
( avg( nullif( voltage_1, 0 )) = 0 OR avg( nullif( voltage_1, 0 )) IS NULL, 0, 1 )+
IF
( avg( nullif( voltage_2, 0 )) = 0 OR avg( nullif( voltage_2, 0 )) IS NULL, 0, 1 )+
IF
( avg( nullif( voltage_2, 0 )) = 0 OR avg( nullif( voltage_3, 0 )) IS NULL, 0, 1 )+
IF
( avg( nullif( voltage_sum, 0 )) = 0 OR avg( nullif( voltage_sum, 0 )) IS NULL, 0, 1 )),
2
) AS voltage_sum
FROM
power
WHERE
room_id = 214