Sql server 按在SQL Server 2000中工作而不是在SQL Server 2005中工作分组
我在SQLServer2000中使用了以下查询Sql server 按在SQL Server 2000中工作而不是在SQL Server 2005中工作分组,sql-server,Sql Server,我在SQLServer2000中使用了以下查询 SELECT U.FirstName, SUM(VE.Score)AS Score, SUM(VE.QuizTime) AS Time, SUM(VE.IsQuizType) AS QuizesAttempted, SUM(VE.IsProgrammingType) AS ProgrammingProblemsAttempted from Users U INNER JOIN VirtualExercise
SELECT
U.FirstName,
SUM(VE.Score)AS Score, SUM(VE.QuizTime) AS Time,
SUM(VE.IsQuizType) AS QuizesAttempted,
SUM(VE.IsProgrammingType) AS ProgrammingProblemsAttempted
from
Users U INNER JOIN VirtualExercise VE on U.UserID=VE.UserID
where U.UserID IN( 10,11 ) AND ProgramID = 2
group by U.FirstName
order by VE.Score desc
它在SQLServer2000中工作正常,但在SQLServer2005中不工作。
给出以下错误:
“VirtualExercise.Score”列是
在ORDER BY子句中无效,因为
它不包含在任何一个
聚合函数或分组方式
条款。---内部异常
请帮忙 SQL Server 2005是正确的:您试图按输出中不存在的值排序,因为您必须对该值进行分组或聚合<代码>VE。分数两者都不是。SQL Server 2000允许一些类似的歧义(编辑:请参阅中的“ORDER BY子句”以了解有关信息) 我想你是说
ORDER BY SUM(VE.Score) DESC
SQL Server 2005是正确的:您试图按输出中不存在的值排序,因为您必须对该值进行分组或聚合<代码>VE。分数两者都不是。SQL Server 2000允许一些类似的歧义(编辑:请参阅中的“ORDER BY子句”以了解有关信息) 我想你是说
ORDER BY SUM(VE.Score) DESC
既然您在select中将
SUM(VE.Score)定义为Score
,那么您的意思是ORDER BY Score DESC
既然您在select中将SUM(VE.Score)定义为Score
,那么您的意思是ORDER BY Score DESCSQL Server 2000在结果集中的表和列别名方面有点愚蠢。如果引入了明确的别名列(例如,在您的情况下,Score
,在下面的示例中,Col3
),则在ORDER BY子句中使用什么表别名无关紧要:
create table #T1 (
ID int not null primary key,
Col1 varchar(10) not null
)
go
create table #T2 (
ID int not null primary key,
Col2 varchar(10) not null
)
go
insert into #T1 (ID,Col1)
select 1,'abc' union all
select 2,'def'
go
insert into #T2 (ID,Col2)
select 1,'zyx' union all
select 2,'wvu'
go
select *,1 as Col3 from #T1 t1 inner join #T2 t2 on t1.ID = t2.ID order by t2.Col3
由于您可能希望按计算的分数
列进行排序,因此只需删除VE.
前缀。SQL Server 2000在结果集中的表别名和列别名有点傻。如果引入了明确的别名列(例如,在您的情况下,Score
,在下面的示例中,Col3
),则在ORDER BY子句中使用什么表别名无关紧要:
create table #T1 (
ID int not null primary key,
Col1 varchar(10) not null
)
go
create table #T2 (
ID int not null primary key,
Col2 varchar(10) not null
)
go
insert into #T1 (ID,Col1)
select 1,'abc' union all
select 2,'def'
go
insert into #T2 (ID,Col2)
select 1,'zyx' union all
select 2,'wvu'
go
select *,1 as Col3 from #T1 t1 inner join #T2 t2 on t1.ID = t2.ID order by t2.Col3
由于您可能希望按计算的分数
列进行排序,只需删除VE.
前缀。是的,这也是我所指的是的,这也是我所指的,但我的查询在SQL Server 2000中运行得很好。它在SSQLServer2005中只给出了一个错误。但是我的查询在SQLServer2000中运行得很好。它在SSQLServer2005中给出的唯一错误。