gpt4 book ai didi

sql - 列在选择列表中无效,因为它未包含在聚合函数或 GROUP BY 子句中

转载 作者:行者123 更新时间:2023-12-03 00:26:57 25 4
gpt4 key购买 nike

我有下面的 sql 查询,但执行时遇到问题。

SELECT * from (Select row_number() OVER(Order By FloorUserId) as 'row_number', FloorUserId,
max(CASE WHEN AreaId='[G]' or AreaId=N'L01' THEN 'X' ELSE ' ' END) as 'L01',
max(CASE WHEN AreaId='[G]' or AreaId=N'L02' THEN 'X' ELSE ' ' END) as 'L02'
from floor, tbuser where FloorUserId= tbuser.userID
) as derivedTable where row_number BETWEEN 1 AND 20

但我不断收到以下错误:

Column 'FloorId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

最佳答案

  • 您有用于聚合的 MAX,因此您需要 GROUP BY Id
  • ...这将不起作用,因为您有 ROW_NUMBER
  • 您确实想要楼层和用户之间存在笛卡尔积(CROSS JOIN)吗?
  • 哪个列属于哪个表?

也许这可以帮助你到达你想要的地方:

Select
row_number() OVER (PARTITION BY userid Order By user.Id) as 'row_number', user.Id,
max(CASE WHEN floor.AreaId='[G]' or floor.AreaId=N'L01' THEN 'X' ELSE ' ' END) as 'L01',
max(CASE WHEN floor. AreaId='[G]' or floor.AreaId=N'L02' THEN 'X' ELSE ' ' END) as 'L02'
from
floor
JOIN
user ON floor. = user. --what?
where
user.Id = userID
group by
user.Id

关于sql - 列在选择列表中无效,因为它未包含在聚合函数或 GROUP BY 子句中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5114112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com