SQL Server:Group By导致“列无效”错误,该如何解决?

时间:2019-02-06 05:32:42

标签: sql sql-server

我正在尝试根据上次更新的机会(使用cg_group)来过滤GROUP BY名称(请检查查询)并分组(使用:ORDER BY opportunities.date_modified DESC)结果。

当我使用不带group的查询时,它返回以下结果:

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行
  列“ opportunities.date_modified”在ORDER BY子句中无效,因为它既不在聚合函数中也不在GROUP BY子句中。

请帮助我解决此问题,谢谢。

3 个答案:

答案 0 :(得分:0)

opportunities.date_modified放入选择并进行分组,然后可以按以下顺序使用

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

无需分组,因为您没有使用任何汇总函数

答案 1 :(得分: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;

请注意,这可能不完全是您想要的。此答案为每个名称组返回一条记录,该记录是该名称组的最新更新。然后,它将所有结果降序排列,但也许您想升序。

答案 2 :(得分:0)

仅在您的distinct语句之后添加SELECT的方式。

Select distinct ... from ...