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

发布于 2024-10-19 07:15:08 字数 522 浏览 5 评论 0原文

我下面有 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

但我不断收到以下错误:

列“FloorId”在选择中无效 列出,因为它不包含在 聚合函数或 GROUP BY 子句。

I have sql query below but i face a problem when execute it.

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

But I keep getting the following error:

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

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

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

发布评论

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

评论(2

ペ泪落弦音 2024-10-26 07:15:08
  • 您有用于聚合的 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
  • You have MAX which is for aggregates so you'd need GROUP BY Id
  • ...this won't then work because you have ROW_NUMBER
  • Do you really want a Cartesian product (CROSS JOIN) between floor and user?
  • what column belongs to what table?

Perhaps this may help you to get where you want:

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
墟烟 2024-10-26 07:15:08

当您使用聚合(如 max)时,只能使用属于 group by 子句一部分的字段。

因此,如果您希望其他字段将它们添加到 group by 子句中,请去掉“*”。

You can only use fields that are part of a group by clause when you use aggregrates (like max).

So get rid of the '*' if you want other fields add them into a group by clause.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文