MYSQL:来自参数的条件GROUP BY?

时间:2017-11-06 16:54:47

标签: mysql

我试图编写一个按参数变量分组的过程,但此时,对我来说这是不可能的。它可以做或者我写任何错误吗?

这个过程的最后一步是检索一个值列表,按时间段分组,按行分组,它可以正常工作。 (粒度是一个过程参数)

                SET g = CASE granularity 
                    WHEN  'week' THEN 'WEEK(r.value_date,1),YEAR(r.value_date)'
                    WHEN  'month' THEN 'MONTH(r.value_date,1),YEAR(r.value_date)'
                    ELSE 'r.value_date' 
                END;

                INSERT INTO system_temp_points_data (`method`,`value`,`num_rows`,value_date,ref,units_id,unit_name,`hash`,granularity) SELECT
                    'sum' AS `method`,
                    SUM(`value`) AS `value`,
                    COUNT(`value`) AS num_rows,
                    UNIX_TIMESTAMP(MIN(value_date)) AS value_date,
                    GROUP_CONCAT(raw_data_id) AS ref,
                    r.units_id,
                    u.unit_name,
                    aux_hash,
                    granularity
                FROM
                    raw_data r
                JOIN temp_unit_group t ON (r.units_id = t.units_id)
                JOIN units u ON (t.units_id = u.units_id)
                AND (t.start_date <= r.value_date)
                AND (t.end_date >= r.value_date)
                WHERE
                    r.value_date BETWEEN start_date
                AND end_date
                AND kpis_id = kpi
                AND IF(exclude_zeros = 1,`value` <> '0',true)
                AND IF(value_aux IS NOT NULL,r.aux = value_aux,true)
                GROUP BY g  
                ORDER BY
                    r.value_date ASC;

1 个答案:

答案 0 :(得分:1)

g中的字符串在查询中使用时不会被评估。您需要将该条件放在查询本身中:

GROUP BY CASE granularity
        WHEN 'week' THEN WEEK(r.value_date,1)
        WHEN 'month' THEN MONTH(r.value_date)
        ELSE r.value_date
    END,
    CASE granularity
        WHEN 'week' THEN YEAR(r.value_date)
        WHEN 'month' THEN YEAR(r.value_date)
        ELSE 1
    END

另一种方法是使用预准备语句,并将g合并到其中:

SET @sql = CONCAT("INSERT INTO system_temp_points_data (`method`,`value`,`num_rows`,value_date,ref,units_id,unit_name,`hash`,granularity) SELECT
                'sum' AS `method`,
                SUM(`value`) AS `value`,
                COUNT(`value`) AS num_rows,
                UNIX_TIMESTAMP(MIN(value_date)) AS value_date,
                GROUP_CONCAT(raw_data_id) AS ref,
                r.units_id,
                u.unit_name,
                aux_hash,
                granularity
            FROM
                raw_data r
            JOIN temp_unit_group t ON (r.units_id = t.units_id)
            JOIN units u ON (t.units_id = u.units_id)
            AND (t.start_date <= r.value_date)
            AND (t.end_date >= r.value_date)
            WHERE
                r.value_date BETWEEN start_date
            AND end_date
            AND kpis_id = kpi
            AND IF(exclude_zeros = 1,`value` <> '0',true)
            AND IF(value_aux IS NOT NULL,r.aux = value_aux,true)
            GROUP BY ", g, "  
            ORDER BY
                r.value_date ASC";
PREPARE @stmt FROM @sql;
EXECUTE @stmt;