这个查询得到的数据有错,高手快来帮忙,(老大 wgsasd311(自强不息)快来,高分回报)
wgsasd311(自强不息)老大,这个查询当初你写的,有问题啦
现在产生问题了,统计出的数据有偏差
create table #paiming(paiming int,stuname varchar(10),stuid int,classid int ,tatolscore decimal(9,2))
insert into tb
select 4,'张三',101,1,504.0 union all
select 6,'李四',102,1,485.0 union all
select 2,'王五',103,1,530.0 union all
select 1,'小白',104,1,544.0 union all
select 3,'宋华',105,1,523.0 union all
select 5,'钱一',106,1,486.0 union all
select 1,'赵善',201,2,558.0 union all
select 2,'陈锋',202,2,521.0
go
(数据你可以多输入几个就能看出问题了)
查询语句如下:主要在两个查询集的left join是产生错误的
这应该怎么改?最初我测试的数据很少还没有发现问题
现在数据多了马上产生问题了
declare @s varchar(8000),@s2 varchar(8000)
set @s='select a.*,b.年级 from (select top 10 [名次]=paiming '
select @s=@s+',['+cast(classid as varchar)+'班]=max(case classid when '+cast(classid as varchar)
+ ' then stuname + ''(''+cast(tatolscore as nvarchar)+'')'' else '''' end)'
from #paiming group by classid
--print @s
set @s=@s+' from #paiming group by paiming )a '
set @s2=' left join (select top 10 名次=(select count(1) from #paiming where tatolscore>=a.tatolscore ),
年级=stuname+''(''+cast(classid as varchar)+''班)'' from #paiming a ) b on a.名次=b.名次 order by a.名次'
exec(@s+@s2+' drop table #t drop table #paiming')
go
老大们快点帮忙解决,急啊
就要给人检查我的成果了
问题点数:100、回复次数:3Top
1 楼splory(爽儿)回复于 2006-03-04 11:55:37 得分 0
我现在倒入的数据多点了,得到的结果却是:
1 小白(544.5) 张少锋(562) 张少锋(2班)
2 张军(538) 赵善(558.5) NULL
3 钱一(534) 谢晖(534) 小白(1班)
4 王五(531) 韩立(518) 张军(1班)
5 王敏(529) 柏树(506) NULL
6 宋华(523.5) 甄兆元(499) 钱一(1班)
7 欧兰(505) 陈敏(470) 王五(1班)
8 张三(504) 陈建(424) 王敏(1班)
9 李四(485) 杨焕(381) 宋华(1班)
两个NULL值都产生了,这就是left join产生的阿Top
2 楼wds1728(wds)回复于 2006-03-04 16:16:59 得分 100
正解
declare @s varchar(8000),@s2 varchar(8000)
set @s='select a.*,b.年级 from (select top 10 [名次]=paiming '
select @s=@s+',['+cast(classid as varchar)+'班]=max(case classid when '+cast(classid as varchar)
+ ' then stuname + ''(''+cast(score as nvarchar)+'')'' else '''' end)'
from #paiming group by classid
--print @s
set @s=@s+' from #paiming group by paiming )a '
set @s2=' inner join (select top 10 名次=(select count(1) from #paiming where score>=a.score ),
年级=stuname+''(''+cast(classid as varchar)+''班)'' from #paiming a order by 名次) b on a.名次=b.名次 order by a.名次'
exec(@s+@s2+' drop table #t drop table #paiming')
go
这个问题只需对后面各查询集做个排序就成Top
3 楼wgsasd311(自强不息)回复于 2006-03-04 16:40:33 得分 0
--try
create table #paiming(paiming int,stuname varchar(10),
stuid int,classid int ,
tatolscore decimal(9,2))
insert into #paiming
select 4,'张三',101,1,504.0 union all
select 6,'李四',102,1,485.0 union all
select 2,'王五',103,1,530.0 union all
select 1,'小白',104,1,544.0 union all
select 3,'宋华',105,1,523.0 union all
select 5,'钱一',106,1,486.0 union all
select 1,'赵善',201,2,558.0 union all
select 2,'陈锋',202,2,521.0
go
/*(数据你可以多输入几个就能看出问题了)
查询语句如下:主要在两个查询集的left join是产生错误的
这应该怎么改?最初我测试的数据很少还没有发现问题
现在数据多了马上产生问题了*/
declare @s varchar(8000),@s2 varchar(8000)
set @s='select a.*,b.年级 from (select top 10 [名次]=paiming '
select @s=@s+',['+cast(classid as varchar)+'班]=max(case classid when '+cast(classid as varchar)
+ ' then stuname + ''(''+cast(tatolscore as nvarchar)+'')'' else '''' end)'
from #paiming group by classid
set @s=@s+' from #paiming group by paiming order by paiming )a '
--print @s
set @s2=' left join (select top 10 名次=(select count(1) from #paiming where tatolscore>=a.tatolscore ),
年级=stuname+''(''+cast(classid as varchar)+''班)'' from #paiming a order by tatolscore desc) b on a.名次=b.名次 order by a.名次'
--print @s2
exec(@s+@s2+' drop table #paiming')
go
Top




