"Each GROUP BY expression must contain at least one column that is not an outer reference" 和 "grouping set"

"Each GROUP BY expression must contain at least one column that is not an outer reference" with "grouping set"

我有以下 SQL 查询,我的目标是进行小计和总计,按产品对小计进行分组,但我的问题是如果我只使用 "group by"他将不得不添加 "select" 的所有字段,结果将不是所需的结果。所以我尝试使用"grouping set",但它给出了以下错误

"Each GROUP BY expression must contain at least one column that is not an outer reference"

有没有人能解决上述问题或只能按产品分组?

SELECT 
    p.id_product,
    p.name,
    dd.name ,
    isnull(p.internal code, '------'),
    {fn CONCAT(CAST(d.id_division AS varchar), {fn CONCAT(' - ', d.name)}) },
    sum(vs.value1),
    sum(p.value2),
    sum(p.value2 * vs.value1)
FROM product p
    LEFT JOIN division d on d.id_division  = p.id_division
    LEFT JOIN division2 dd on dd.id_product = p.id_product
    LEFT JOIN value_stockroom vs on vs.id_product = p.id_product and vs.id_division2 = dd.id_division2
GROUP BY
    GROUPING SETS((p.id_product, p.name), (dd.name), (p.internal_code), (d.id_division, d.name), NULL)
ORDER BY 
    p.name, p.id_product, d.name

我是使用 Grouping set 子句的新手

NULLGROUP BY 中是不允许的,因为它是一个常量。你想要空括号:

GROUP BY GROUPING SETS( (p.id_product, p.name), 
                        (dd.name),
                        (p.internal_code),
                        (d.id_division, d.name),
                        ()
                      )