分段求和

SELECT  device_code,
        collect_Date AS DAY_OF_MONTH_KEY, 
        sum(CASE WHEN COLLECT_HOUR BETWEEN 7 AND 21 THEN incremental ELSE 0 END ) AS  PEAK_VALUE, 
        sum(CASE WHEN COLLECT_HOUR <7 OR COLLECT_HOUR >21 THEN incremental ELSE 0 END) AS  VALLY_VALUE,
        0 AS COMMON_VALUE,
        sum(CASE WHEN COLLECT_HOUR BETWEEN 8 AND 21 THEN incremental ELSE 0 END) AS DAY_DAYTIME_VALUE,
        sum(CASE WHEN COLLECT_HOUR <8 OR COLLECT_HOUR >21 THEN incremental ELSE 0 END) AS DAY_NIGHT_VALUE,
        sum(INCREMENTAL) AS TOTAL_DAY_VALUE,
        0 AS TOTAL_YOY
         FROM ELECTRIC_METER GROUP BY COLLECT_DATE,device_code ORDER BY COLLECT_DATE;

你可能感兴趣的:(分段求和)