0

我有一个与Invalid use of group functionMySQL 相关的问题。

我要执行的查询是这样的(我知道这是一个非常大的查询,但如果我能执行这个,那么我将调整和修复性能和易于阅读的内容):

SELECT
    `id_c` AS `sceneId`,
    `scene`.`id` AS `sceneName`,
    `scene`.`frameCount` AS `frames`,
    task.id_mv AS `id_mv`,
    CONCAT( '[',GROUP_CONCAT(
        CONCAT( 
            '{',
                '"idTk": ', `task`.`id_tk`, ', ',
                -- '"tktype": ', `task`.`id_tk_type`, ', ',
                -- '"subDepType": ', IF ( `task`.`id_sub_dep_type` = 54, `task`.`id_sub_dep_type`, 'null'), ', ',
                -- '"color": "', `task`.`color`, '", ',
                '"weight": ', MAX(`task`.`weight`), ', ',
                '"dept": ', '"',
                    CASE `task`.`id_tk_type`
                        WHEN 43 THEN 'MVB'
                        WHEN 12 THEN 'LYT'
                        WHEN 32 THEN
                            CONCAT_WS(
                                ', ',
                                IF( `task`.`id_sub_dep_type` != 54, 'ANI', NULL ),
                                IF( `task`.`id_sub_dep_type` = 54, 'CKD', NULL )
                            )
                        WHEN 190 THEN 'DYN'
                        WHEN 104 THEN 'FIN'
                        WHEN 191 THEN 'LGT'
                    END, '"',
                -- task.id_tk_type, ', ',
                -- task.id_sub_dep_type,            
            '}'
        )
        ORDER BY
            FIELD( `task`.`id_tk_type`, 43, 12, 32, 190, 104, 191 ),
            `task`.`weight` DESC
        SEPARATOR
            ', '
    ), ']') AS `tkInfo`
FROM
        `V_task` AS `task`
        JOIN `tk_fct_mcsc` AS `mcsc` ON `task`.`id_tk` = `mcsc`.`id_tk`
        JOIN `scene` ON `mcsc`.`id_c` = `scene`.`id_scene`
WHERE
    `task`.`id_sub_dep_type` IN (
        SELECT
            `id_sub_dep_type`
        FROM
            `tk_dim_sub_dep_type`
        WHERE `id_tk_type` IN (
            SELECT
                `id_tk_type`
            FROM
                `tk_dim_dep_type`
            WHERE
                `id_tk_type` IN ( 43, 12, 32, 190, 104, 191 )
        )
              AND `id_sub_dep_type` != 54
        UNION
        SELECT
            `id_sub_dep_type`
        FROM
            `tk_dim_sub_dep_type`
        WHERE
            `id_sub_dep_type` = 54
    )
    AND `task`.`is_appr` = FALSE
    AND `mcsc`.`type` = 'sc'
    AND `scene`.`id` != '000'
    -- AND `mcsc`.`id_b` = 530
GROUP BY
    `scene`.`id`,
    `scene`.`id_scene`
    -- task.weight
ORDER BY
    `scene`.`id`,
    `task`.`weight` DESC;

错误,我知道是函数MAX( task.weight )内部GROUP_CONCAT,但我不知道如何避免这件事..

我实际上正在尝试构建某种 sqlfiddle 东西来让您测试这些东西,但我认为我需要的只是另一双让我知道去哪里看的眼睛。

谢谢!

编辑#1 正如kickstart所问,我想要获得的是这样的:

sceneId sceneName   frames  id_mv   tkInfo
200     001         1200    1       "[{"dept": "LGT"}, {"dept": "LGT"}]"
1342    001         45      14      "[{"dept": "LYT"}, {"dept": "LGT"}, {"dept": "LGT"}]"
1335    001         460     14      "[{"dept": "MVB"}, {"dept": "LYT"}, {"dept": "LGT"}, {"dept": "LGT"}]"
1351    001         20      11      "[{"dept": "LYT"}, {"dept": "ANI"}, {"dept": "FIN"}, {"dept": "LGT"}]"
1375    001         63      11      "[{"dept": "LYT"}, {"dept": "CKD"}, {"dept": "ANI"}, {"dept": "DYN"}, {"dept": "FIN"}, {"dept": "FIN"}, {"dept": "FIN"}, {"dept": "LGT"}]"
1382    001         66      11      "[{"dept": "LYT"}, {"dept": "CKD"}, {"dept": "ANI"}, {"dept": "FIN"}]"

但是,如果没有“部门”的重复,并且这样做,我需要那个部门的最大重量。

编辑#2 我用一点作弊解决了我的这个问题。

首先,我简化了很多主要查询,删除了case-thing并为每个sceneId插入了一个MAX(weight)。

通过这个简化的查询,我构建了一个视图,并通过该视图应用了 case/group_concat 的东西..所有结果都很好。

[ ..sqlfiddle 将立即插入此处.. ]

4

1 回答 1

1

有点玩,并且(非常)未经测试,但认为您需要有效地进行两次查询。一次获取详细信息,一次获取最大重量。您可以移动您的一些子选择(进行加入 - 联合将在加入之前处理重复项)。

像这样的东西: -

SELECT
    Sub1.id_c AS sceneId,
    Sub1.id AS sceneName,
    Sub1.frameCount AS frames,
    Sub1.id_mv AS id_mv,
    CONCAT( '[',GROUP_CONCAT(
        CONCAT( 
            '{',
                '"idTk": ', Sub1.id_tk, ', ',
                '"weight": ', Sub2.MaxTaskWeight, ', ',
                '"dept": ', '"', Sub1.id_tk_type_decode, '"',
            '}'
        )
        ORDER BY
            FIELD( Sub1.id_tk_type, 43, 12, 32, 190, 104, 191 ),
            Sub2.MaxTaskWeight DESC
        SEPARATOR
            ', '
    ), ']') AS tkInfo
FROM
(
    SELECT
        id_c AS sceneId,
        scene.id AS sceneName,
        scene.frameCount AS frames,
        task.id_mv AS id_mv,
        task.id_tk, 
        CASE task.id_tk_type
            WHEN 43 THEN 'MVB'
            WHEN 12 THEN 'LYT'
            WHEN 32 THEN IF( task.id_sub_dep_type = 54, 'CKD', 'ANI' )
            WHEN 190 THEN 'DYN'
            WHEN 104 THEN 'FIN'
            WHEN 191 THEN 'LGT'
        END AS id_tk_type_decode,
        task.id_tk_type
    FROM    V_task AS task
    INNER JOIN  tk_fct_mcsc AS mcsc ON task.id_tk = mcsc.id_tk
    INNER JOIN  scene ON mcsc.id_c = scene.id_scene
    INNER JOIN 
    (   
        SELECT id_sub_dep_type
        FROM tk_dim_sub_dep_type 
        INNER JOIN tk_dim_dep_type
        ON tk_dim_sub_dep_type.id_tk_type = tk_dim_dep_type.id_tk_type
        WHERE  tk_dim_dep_type.id_tk_type  IN ( 43, 12, 32, 190, 104, 191 )
        AND tk_dim_sub_dep_type.id_sub_dep_type != 54
        UNION
        SELECT id_sub_dep_type
        FROM tk_dim_sub_dep_type 
        WHERE id_sub_dep_type = 54
    ) Sub1
    ON task.id_sub_dep_type = Sub1.id_sub_dep_type
    WHERE task.is_appr = FALSE
    AND mcsc.`type` = 'sc'
    AND scene.`id` != '000'
)
INNER JOIN
(
    SELECT scene.id, scene.id_scene, MAX(task.weight) AS MaxTaskWeight
    FROM    V_task AS task
    INNER JOIN  tk_fct_mcsc AS mcsc ON task.id_tk = mcsc.id_tk
    INNER JOIN  scene ON mcsc.id_c = scene.id_scene
    INNER JOIN 
    (   
        SELECT id_sub_dep_type
        FROM tk_dim_sub_dep_type 
        INNER JOIN tk_dim_dep_type
        ON tk_dim_sub_dep_type.id_tk_type = tk_dim_dep_type.id_tk_type
        WHERE  tk_dim_dep_type.id_tk_type  IN ( 43, 12, 32, 190, 104, 191 )
        AND tk_dim_sub_dep_type.id_sub_dep_type != 54
        UNION
        SELECT id_sub_dep_type
        FROM tk_dim_sub_dep_type 
        WHERE id_sub_dep_type = 54
    ) Sub1
    ON task.id_sub_dep_type = Sub1.id_sub_dep_type
    WHERE task.is_appr = FALSE
    AND mcsc.`type` = 'sc'
    AND scene.`id` != '000'
    GROUP BY scene.id, scene.id_scene 
) Sub2
ON Sub1.id = Sub2.id
AND Sub1.id_scene = Sub2.id_scene
GROUP BY
    Sub1.id,
    Sub1.id_scene
ORDER BY
    Sub1.id,
    MaxTaskWeight DESC;
于 2013-11-06T14:00:10.140 回答