gpt4 book ai didi

sql - 聚合可能不会出现在 WHERE 子句中(SQL Server 错误)

转载 作者:行者123 更新时间:2023-12-01 13:47:47 24 4
gpt4 key购买 nike

当我尝试这个查询时,

select  
(A.StudentId),
max(A.StudentFirstName),
max(A.StudentLastName),
max(A.StudentAddress),
'Batch ' + max(C.BatchName),
CAST(MAX(CAST(A.StudentStatus as INT)) AS BIT),
max(B.StudentBatchId)
from
tblStudentDetails A
inner join
tblStudentBatchDetails B on A.StudentId = B.studentid
inner join
tblBatch C on C.BatchId = B.batchid
where
max(A.StudentFirstName) like 'A%'
group by
A.StudentId

我遇到了这个错误:

An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.

有人可以帮助解决这个问题吗?

最佳答案

正确的语法是...

select  (A.StudentId),max(A.StudentFirstName),
max(A.StudentLastName),max(A.StudentAddress),
'Batch ' + max(C.BatchName),CAST(MAX(CAST(A.StudentStatus as INT)) AS BIT),
max(B.StudentBatchId)
from tblStudentDetails A
inner join tblStudentBatchDetails B on A.StudentId=B.studentid
inner join tblBatch C on C.BatchId=B.batchid
group by A.StudentId
having max(A.StudentFirstName) like 'A%'

关于sql - 聚合可能不会出现在 WHERE 子句中(SQL Server 错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36375099/

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