SQL服务器:分组的原因“列无效”错误,如何解决?

问题描述 投票:0回答:3

我想筛选cg_group名称(请查询)和组(使用:GROUP BY)根据最近更新的机会的结果(使用:ORDER BY opportunities.date_modified DESC)。

当我使用的查询,而无需使用GROUP BY返回的结果如下:

SELECT cg_groups.name
FROM cg_groups
JOIN cg_groups_cstm ON cg_groups_cstm.id_c = cg_groups.id
JOIN accounts_cstm ON cg_groups.name = accounts_cstm.client_group_c
JOIN accounts ON accounts.id = accounts_cstm.id_c
JOIN accounts_opportunities ON accounts.id = accounts_opportunities.account_id
JOIN opportunities ON accounts_opportunities.opportunity_id = opportunities.id
WHERE cg_groups.deleted='0' AND cg_groups_cstm.status_c='1' AND opportunities.deleted='0' 
ORDER BY opportunities.date_modified DESC

结果:

ABC Group
ABC Group
CBC Group
ABC Group
XYZ Group

但我想这组顺序如下:

ABC Group
CBC Group
XYZ Group

要做到这一点,我添加GROUP BY cg_groups.name

SELECT cg_groups.name
FROM cg_groups
JOIN cg_groups_cstm ON cg_groups_cstm.id_c = cg_groups.id
JOIN accounts_cstm ON cg_groups.name = accounts_cstm.client_group_c
JOIN accounts ON accounts.id = accounts_cstm.id_c
JOIN accounts_opportunities ON accounts.id = accounts_opportunities.account_id
JOIN opportunities ON accounts_opportunities.opportunity_id = opportunities.id
WHERE cg_groups.deleted='0' AND cg_groups_cstm.status_c='1' AND opportunities.deleted='0' 
GROUP BY cg_groups.name
ORDER BY opportunities.date_modified DESC

但现在我得到这个错误:

消息8127,级别16,状态1,行10 因为它不是在聚合函数或GROUP BY子句中包含列“opportunities.date_modified”是在ORDER BY子句无效。

有人请帮我解决这个问题,谢谢。

sql sql-server
3个回答
0
投票

使用ROW_NUMBER找到每个组的最近更新的记录:

WITH cte AS (
    SELECT cg_groups.name, o.date_modified,
        ROW_NUMBER() OVER (PARTITION BY o.date_modified DESC) rn
    FROM cg_groups cg
    INNER JOIN cg_groups_cstm cgc
        ON cgc.id_c = cg.id
    INNER JOIN accounts_cstm ac
       ON cg.name = ac.client_group_c
    INNER JOIN accounts a
       ON a.id = ac.id_c
    INNER JOIN accounts_opportunities ao
        ON a.id = ao.account_id
    INNER JOIN opportunities o
        ON ao.opportunity_id = o.id
    WHERE cg.deleted = '0' AND cgc.status_c = '1' AND o.deleted = '0'
)

SELECT name
FROM cte
WHERE rn = 1
ORDER BY date_modified DESC;

请注意,这可能不是你想要什么。这个答案返回每名组是最近更新的该组的单个记录。然后,它令所有的结果下降,但也许你想上升。


0
投票

opportunities.date_modified在选择和group by,那么你可以通过使用,为了

SELECT opportunities.date_modified,cg_groups.name
FROM cg_groups
JOIN cg_groups_cstm ON cg_groups_cstm.id_c = cg_groups.id
JOIN accounts_cstm ON cg_groups.name = accounts_cstm.client_group_c
JOIN accounts ON accounts.id = accounts_cstm.id_c
JOIN accounts_opportunities ON accounts.id = accounts_opportunities.account_id
JOIN opportunities ON accounts_opportunities.opportunity_id = opportunities.id
WHERE cg_groups.deleted='0' AND cg_groups_cstm.status_c='1' AND opportunities.deleted='0' 
GROUP BY cg_groups.name,opportunities.date_modified
ORDER BY opportunities.date_modified DESC

但你的结果,你可以尝试像下面只是使用不同

SELECT distinct cg_groups.name
FROM cg_groups
JOIN cg_groups_cstm ON cg_groups_cstm.id_c = cg_groups.id
JOIN accounts_cstm ON cg_groups.name = accounts_cstm.client_group_c
JOIN accounts ON accounts.id = accounts_cstm.id_c
JOIN accounts_opportunities ON accounts.id = accounts_opportunities.account_id
JOIN opportunities ON accounts_opportunities.opportunity_id = opportunities.id
WHERE cg_groups.deleted='0' AND cg_groups_cstm.status_c='1' AND opportunities.deleted='0'
order by cg_groups.name

没有组的需求,因为你还没有使用任何聚合函数


0
投票

怎么样你distinct声明之后只是增加SELECT

Select distinct ... from ...

推荐问答