Sql 使用GROUPBY子句
查询: 获取以下错误: Msg 8120,16级,状态1,第2行 列“studemo.ident”在中无效 选择列表,因为它不是 包含在聚合中的 函数或GROUPBY子句Sql 使用GROUPBY子句,sql,sql-server,group-by,Sql,Sql Server,Group By,查询: 获取以下错误: Msg 8120,16级,状态1,第2行 列“studemo.ident”在中无效 选择列表,因为它不是 包含在聚合中的 函数或GROUPBY子句 我的目标是获得最新的考试成绩。在我尝试添加更多表以包含更多学生信息之前,我是成功的。为了使SQL语句有效,您需要将聚合函数中不包含的列添加到GROUP BY中 分组依据sd.ident、sd.suniq、testc、subtestc、ts.testscore、metadept、ts.takent您需要将未使用聚合函数的剩余字段
我的目标是获得最新的考试成绩。在我尝试添加更多表以包含更多学生信息之前,我是成功的。为了使SQL语句有效,您需要将聚合函数中不包含的列添加到GROUP BY中
分组依据sd.ident、sd.suniq、testc、subtestc、ts.testscore、metadept、ts.takent您需要将未使用聚合函数的剩余字段添加到分组依据中
注释:上面的代码是@Neil的意思我怀疑您希望显示每个学生的最大考试次数,即使他们参加了多次考试,例如,如果他们参加了两次考试,那么您希望显示两次考试日期,并且在每行中显示最大日期。如果是这样的话,那么按所有其他列进行分组将没有帮助。假设SQL Server 2005或更高版本,可以尝试以下操作:
select sd.ident,sd.suniq, testc, subtestc, ts.testscore, metadept, ts.takendt, max(takendt)testdate
from studemo sd, stutests ts, testdef td, udefstu ud
where ts.suniq =sd.suniq
and td.testuniq = ts.testuniq
and ts.suniq = ud.suniq
and td.testuniq IN ('2000089', '2000090', '2000091', '2000092')
group by sd.ident,sd.suniq, testc, subtestc, ts.testscore, metadept, ts.takendt
order by suniq
我根据以下建议更改了查询:您需要将不在聚合函数中的列添加到GROUP BY中,以使SQL语句有效。按sd.ident、sd.suniq、testc、subtestc、ts.testscore、metadept、ts.takendtOk分组,我怀疑结果不会很理想,因为所有这些列值的每个排列都会有一行,但谁知道呢……我的testuniq指示了测试的级别。一旦水平通过,学生将参加下一个水平测试。因此,当我运行查询时,我会看到所有级别的结果,而我只想看到最后进行的测试。有没有一种方法可以修改查询来做到这一点???您需要提供一些示例数据和所需的结果。
select sd.ident,sd.suniq, testc, subtestc, ts.testscore, metadept, ts.takendt, max(takendt)testdate
from studemo sd, stutests ts, testdef td, udefstu ud
where ts.suniq =sd.suniq
and td.testuniq = ts.testuniq
and ts.suniq = ud.suniq
and td.testuniq IN ('2000089', '2000090', '2000091', '2000092')
group by sd.suniq, sd.ident, testc, subtestc, ts.testscore, metadept, ts.takendt
order by suniq
select sd.ident,sd.suniq, testc, subtestc, ts.testscore, metadept, ts.takendt, max(takendt)testdate
from studemo sd, stutests ts, testdef td, udefstu ud
where ts.suniq =sd.suniq
and td.testuniq = ts.testuniq
and ts.suniq = ud.suniq
and td.testuniq IN ('2000089', '2000090', '2000091', '2000092')
group by sd.ident,sd.suniq, testc, subtestc, ts.testscore, metadept, ts.takendt
order by suniq
WITH md AS
(
SELECT ts.suniq, maxdate = MAX(ts.takendt)
FROM dbo.stutests AS ts
INNER JOIN
dbo.testdef AS td
ON td.testuniq = ts.testuniq
WHERE
td.testuniq IN ('2000089', '2000090', '2000091', '2000092')
GROUP BY ts.suniq
)
SELECT
sd.ident,
sd.suniq,
testc, -- which table does this come from? why no prefix?
subtestc, -- which table does this come from? why no prefix?
ts.testscore,
metadept, -- which table does this come from? why no prefix?
ts.takendt,
md.maxdate
FROM
dbo.studemo AS sd
INNER JOIN
dbo.stutests AS ts
ON sd.suniq = ts.suniq
INNER JOIN
dbo.testdef AS td
ON td.testuniq = ts.testuniq
INNER JOIN
dbo.udefstu AS ud
ON ts.suniq = ud.suniq
INNER JOIN md
ON md.suniq = sd.suniq
WHERE
td.testuniq IN ('2000089', '2000090', '2000091', '2000092')
ORDER BY
sd.suniq; -- you forgot a prefix here too
-- could cause problems if you change the query later