【问题标题】:Cannot do inner join on an select, invalid in the select list无法对选择进行内部连接,在选择列表中无效
【发布时间】:2018-10-30 10:50:05
【问题描述】:

我有一个存储过程,它构建一个临时表并在最后执行一个选择。只要我不包含 l.beskrivning ,它就可以正常工作,这是一个将国家代码映射到国家名称的表。该过程是有效的,但是当我测试它时,我收到以下错误:

列“LK.b”在选择列表中无效,因为它是 不包含在聚合函数或 GROUP BY 子句中。

SELECT TOP 100 tmp.BuyerID, tmp.BuyerNumber, tmp.BuyerName, tmp.BuyerAddress, tmp.BuyerCountryCode, l.b
    FROM #tempGL AS tmp
    INNER JOIN LK AS l ON l.land_id LIKE tmp.BuyerCountryCode
    GROUP BY tmp.BuyerID, tmp.BuyerNumber, tmp.BuyerName, tmp.BuyerAddress, tmp.BuyerCountryCode
    ORDER BY Count(*) DESC, tmp.BuyerName

我也尝试过完全删除 GROUP BY 和 ORDER BY 但我仍然遇到同样的错误。我在这里做错了什么?

【问题讨论】:

  • Select TOP * 不是 MySQL 语法!使用您正在使用的正确 RDBMS 重新标记。

标签: sql sql-server group-by


【解决方案1】:

您需要在group by 子句中包含l.beskrivning

SELECT TOP 100 tmp.BuyerID, tmp.BuyerNumber, tmp.BuyerName, tmp.BuyerAddress, tmp.BuyerCountryCode, l.beskrivning
    FROM #tempGL AS tmp
    INNER JOIN LK AS l ON l.land_id LIKE tmp.BuyerCountryCode
    GROUP BY tmp.BuyerID, tmp.BuyerNumber, tmp.BuyerName, tmp.BuyerAddress, tmp.BuyerCountryCode,l.beskrivning
    ORDER BY Count(*) DESC, tmp.BuyerName

【讨论】:

  • 谢谢,它解决了我的问题!永远不会找到它!
【解决方案2】:

您不理解错误消息的哪一部分?所有未聚合的列都应该在GROUP BY:

SELECT TOP 100 t.BuyerID, t.BuyerNumber, t.BuyerName, tmp.BuyerAddress, t.BuyerCountryCode, l.beskrivning
FROM #tempGL t INNER JOIN
     LK l
     ON l.land_id LIKE t.BuyerCountryCode
GROUP BY t.BuyerID, t.BuyerNumber, t.BuyerName, t.BuyerAddress, t.BuyerCountryCode, l.beskrivning
ORDER BY Count(*) DESC, t.BuyerName

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    相关资源
    最近更新 更多