左加入后无法分组

发布于 2024-08-15 18:03:23 字数 874 浏览 5 评论 0原文

这是我的查询(来自触发器):

UPDATE QuoteItemsGroupFeature
SET Cost = (QuoteItemsGroup.BaseCost + QuoteItemsGroup.AccumulatedCost + 
    ISNULL(SUM(ParentQuoteItemsGroupFeature.Cost), 0)) * INSERTED.Amount
FROM QuoteItemsGroupFeature INNER JOIN INSERTED
    ON QuoteItemsGroupFeature.QuoteItemsGroupFeatureId = 
    INSERTED.QuoteItemsGroupFeatureId
INNER JOIN QuoteItemsGroup ON QuoteItemsGroup.QuoteItemsGroupId = 
    INSERTED.QuoteItemsGroupId
LEFT OUTER JOIN QuoteItemsGroupFeature ParentQuoteItemsGroupFeature ON 
    ParentQuoteItemsGroupFeature.QuoteItemsGroupFeatureId = 
    INSERTED.ParentQuoteItemsGroupFeatureId
GROUP BY QuoteItemsGroup.BaseCost, 
    QuoteItemsGroup.AccumulatedCost, 
    INSERTED.Feature, 
    INSERTED.Cost

它说:

Msg 156, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'GROUP'.

Here is my query (from a trigger):

UPDATE QuoteItemsGroupFeature
SET Cost = (QuoteItemsGroup.BaseCost + QuoteItemsGroup.AccumulatedCost + 
    ISNULL(SUM(ParentQuoteItemsGroupFeature.Cost), 0)) * INSERTED.Amount
FROM QuoteItemsGroupFeature INNER JOIN INSERTED
    ON QuoteItemsGroupFeature.QuoteItemsGroupFeatureId = 
    INSERTED.QuoteItemsGroupFeatureId
INNER JOIN QuoteItemsGroup ON QuoteItemsGroup.QuoteItemsGroupId = 
    INSERTED.QuoteItemsGroupId
LEFT OUTER JOIN QuoteItemsGroupFeature ParentQuoteItemsGroupFeature ON 
    ParentQuoteItemsGroupFeature.QuoteItemsGroupFeatureId = 
    INSERTED.ParentQuoteItemsGroupFeatureId
GROUP BY QuoteItemsGroup.BaseCost, 
    QuoteItemsGroup.AccumulatedCost, 
    INSERTED.Feature, 
    INSERTED.Cost

It says:

Msg 156, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'GROUP'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷雾森÷林ヴ 2024-08-22 18:03:23

您必须将聚合删除到派生表(或 CTE),

我认为这就是您想要的,而且我认为您的分组方式也是错误的。我还添加了别名以使其更易于阅读(无论如何对我来说)

UPDATE
    QF
SET
    Cost = foo.Cost
FROM
    QuoteItemsGroupFeature QF JOIN 
    (
    SELECT
        I.QuoteItemsGroupFeatureId,
        (Q.BaseCost + Q.AccumulatedCost + ISNULL(SUM(P.Cost), 0)) * I.Amount 
            AS Cost
    FROM
        INSERTED I
        JOIN
        QuoteItemsGroup Q ON Q.QuoteItemsGroupId = I.QuoteItemsGroupId
        LEFT JOIN
        QuoteItemsGroupFeature P ON 
            P.QuoteItemsGroupFeatureId = I.ParentQuoteItemsGroupFeatureId
    GROUP BY
        Q.BaseCost, 
        Q.AccumulatedCost, 
        --I.Feature, ??
        --I.Cost, ??
        I.Amount
    ) foo ON QF.QuoteItemsGroupFeatureId = foo.QuoteItemsGroupFeatureId

You have to remove the aggregate to a derived table (or CTE)

I think this is what you want, and I think your group by is wrong too. I've also added aliases to make it easier to read (for me anyway)

UPDATE
    QF
SET
    Cost = foo.Cost
FROM
    QuoteItemsGroupFeature QF JOIN 
    (
    SELECT
        I.QuoteItemsGroupFeatureId,
        (Q.BaseCost + Q.AccumulatedCost + ISNULL(SUM(P.Cost), 0)) * I.Amount 
            AS Cost
    FROM
        INSERTED I
        JOIN
        QuoteItemsGroup Q ON Q.QuoteItemsGroupId = I.QuoteItemsGroupId
        LEFT JOIN
        QuoteItemsGroupFeature P ON 
            P.QuoteItemsGroupFeatureId = I.ParentQuoteItemsGroupFeatureId
    GROUP BY
        Q.BaseCost, 
        Q.AccumulatedCost, 
        --I.Feature, ??
        --I.Cost, ??
        I.Amount
    ) foo ON QF.QuoteItemsGroupFeatureId = foo.QuoteItemsGroupFeatureId
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文