0

我正在为这个 SQL 咨询而苦苦挣扎,错误消息是:

1055 - SELECT 列表的表达式 #10 不在 GROUP BY 子句中,并且包含在功能上不依赖于 GROUP BY 子句中的列的非聚合列 'ctrl2019.s.cgsc_cuenta';这与 sql_mode=only_full_group_by 不兼容。

这是代码:

SELECT c.cgcu_cuenta AS id,
            g.ger_cuenta, g.ger_nombre,
            p.cgp_cuenta, p.cgp_nombre,
            r.rub_cuenta, r.rub_nombre,
            c.cgcu_cuenta, c.cgcu_nombre,
            s.cgsc_cuenta, s.cgsc_nombre,
            SUM(IFNULL(a.debe, 0)) - SUM(IFNULL(a.haber, 0)) AS debe, 0 AS haber,
            'D' AS nat_id,
            c.cgcu_cuenta AS cuenta, c.cgcu_nombre AS nombre
        FROM ctrl2019.cat_Genero g
            INNER JOIN ctrl2019.cat_CgGrupo p USING(ger_id)
            INNER JOIN ctrl2019.cat_Rubro r USING(ger_id, cgp_id)
            INNER JOIN ctrl2019.cat_CgCuenta c USING(ger_id, cgp_id, rub_id)
            INNER JOIN ctrl2019.cat_CgSubcuenta s USING(ger_id, cgp_id, rub_id, cgcu_id)
            LEFT JOIN (
                SELECT a.ger_id, a.grp_id, a.rub_id, a.cta_id, a.sct_id,
                    SUM(IFNULL(a.msl_debe, 0)) AS debe, SUM(IFNULL(a.msl_haber, 0)) AS haber
                FROM ldf.vin_EntePublicoLDF e

                    INNER JOIN ldf.blz_Mensual_2019 a USING(gpo_id, ur_id)
                WHERE e.ejr_id = 2019
                    AND a.ger_id = 1
                    AND e.ent_id = 12
                    AND a.msc_id IN (0, 1, 2, 3)
                GROUP BY a.ger_id, a.grp_id, a.rub_id, a.cta_id, a.sct_id
            )a ON s.ger_id = a.ger_id AND s.cgp_id = a.grp_id AND s.rub_id = a.rub_id AND s.cgcu_id = a.cta_id AND s.cgsc_id = a.sct_id
        WHERE g.ger_id = 1
        GROUP BY g.ger_id, p.cgp_id, r.rub_id, c.cgcu_id;

我不知道为什么它会给我这个错误,我是 sql 的新手。

4

2 回答 2

1

您只需要添加 SELECT 语句中存在的 GROUP BY 中的所有列。

于 2020-02-20T18:18:13.370 回答
0

这里似乎是简单的聚合。我清理了代码并在GROUP BY.

SELECT 
    id = c.cgcu_cuenta
    ,g.ger_cuenta
    ,g.ger_nombre
    ,p.cgp_cuenta
    ,p.cgp_nombre
    ,r.rub_cuenta
    ,r.rub_nombre
    ,c.cgcu_cuenta
    ,c.cgcu_nombre
    ,s.cgsc_cuenta
    ,s.cgsc_nombre
    ,debe = SUM(IFNULL(a.debe, 0)) - SUM(IFNULL(a.haber, 0))
    ,haber = 0
    ,nat_id = 'D'
    ,cuenta = c.cgcu_cuenta
    ,nombre = c.cgcu_nombre
FROM ctrl2019.cat_Genero g
INNER JOIN ctrl2019.cat_CgGrupo p ON USING(ger_id)
INNER JOIN ctrl2019.cat_Rubro r ON USING(ger_id, cgp_id)
INNER JOIN ctrl2019.cat_CgCuenta c USING(ger_id, cgp_id, rub_id)
INNER JOIN ctrl2019.cat_CgSubcuenta s USING(ger_id, cgp_id, rub_id, cgcu_id)
LEFT JOIN (
        SELECT 
            a2.ger_id
            ,a2.grp_id
            ,a2.rub_id
            ,a2.cta_id
            ,a2.sct_id
            ,debe = SUM(IFNULL(a2.msl_debe, 0))
            ,haber = SUM(IFNULL(a2.msl_haber, 0))
        FROM ldf.vin_EntePublicoLDF E
        INNER JOIN ldf.blz_Mensual_2019 a2 USING(gpo_id, ur_id)
        WHERE e.ejr_id = 2019
            AND a2.ger_id = 1
            AND e.ent_id = 12
            AND a2.msc_id IN (0, 1, 2, 3)
        GROUP BY 
            a2.ger_id
            ,a2.grp_id
            ,a2.rub_id
            ,a2.cta_id
            ,a2.sct_id
    ) A 
    ON s.ger_id = A.ger_id 
        AND s.cgp_id = A.grp_id 
        AND s.rub_id = A.rub_id 
        AND s.cgcu_id = A.cta_id 
        AND s.cgsc_id = A.sct_id
WHERE g.ger_id = 1
GROUP BY 
    g.ger_id
    ,p.cgp_id
    ,r.rub_id
    ,C.cgcu_id
    ,c.cgcu_cuenta
    ,g.ger_cuenta
    ,g.ger_nombre
    ,p.cgp_cuenta
    ,p.cgp_nombre
    ,r.rub_cuenta
    ,r.rub_nombre
    ,c.cgcu_nombre
    ,s.cgsc_cuenta
    ,s.cgsc_nombre
于 2020-02-20T18:33:02.747 回答