把myanswer中的第一项取出来插到grade中作为stid???stid不是试题题号,对应t_TQuiz_Detail表中id吗?
解决方案 »
- 本机连接sqlserver2005的连接串?
- sql 2005有没有取得hash值的函数
- 数据类型转换问题?在线等...
- 请问怎样把三天前的记录查出来呢?
- 如何解决sql占用cpu过长的问题
- SQL分布式问题[无法执行该操作,因为链接服务器 "srv2" 的 OLE DB 访问接口 "SQLNCLI" 无法启动分布式事务]
- sql2000 怎么得到表中记录的创建时间和修改时间
- 关于数据库备份的问题?百思不得其解,请知道php和sql的朋友进来
- 请教一个问题:在SQL中如果不使用事务是不是绝对就不会发生死锁????
- Help me! Vpf 中的变量问题!!!!!!!!!!!!!!
- sql 语句问题
- select超级人品问题!help!!!!
--测试表及数据
declare @t table(id int,qid int,accountid int,myanswer nvarchar(1000),myscore nvarchar(1000),score int,testtime datetime)
insert into @t
select 14, 17, 10, '3,2,4,1,3,2,3,2,2,3,3,3,3,2', '3,2,4,1,3,2,3,2,2,3,3,3,3,2', 36, '2002-12-28' union
select 15, 14, 1133, '1,2,2,3,4,2,3,4,3,3', '1,1,0,0,1,0,0,1,0,0' ,4, '2002-12-28' union
select 16 ,17, 1133 ,'2,2,2,2,2,2,2,2,2,2,2,2,2,2' ,'2,2,2,2,2,2,2,2,2,2,2,2,2,2', 28, '2002-12-28' union
select 17 ,14 ,1222, '1,2,2,3,4,2,3,4,3,3', '1,1,0,0,1,0,0,1,0,0' ,4, '2002-12-29' --生成辅助临时表,注意数据行的多少,根据实际修改。
select top 10000 [id]=identity(int,1,1) into #temp
from (select top 100 id from syscolumns) a,
(select top 100 id from syscolumns) b--查询出结果,
select a.accountid,qid,an_id=substring(a.myanswer,b.[id],charindex(',',a.myanswer+',',b.[id])-b.[id])
,score=substring(a.myscore,b.[id],charindex(',',a.myscore+',',b.[id])-b.[id])
from @t a,#temp b
where substring(','+a.myanswer,b.[id],1)=','
order by a.accountid,qid
--删除临时表
drop table #temp
/*结果
自己运行查看。
*/
我关于什么对应关系的描述不清楚?你说的第二个问题其实不难啊,因为它的myanswer的值里第一个数就表示第一道题,第二个就表示第二道,中间用逗号隔开的嘛!
set class = t2.Qtype
from grade t1,t_TQuiz_Detail t2
where t1.item = t2.qid and t1.stid = t2.id
1.昨天怎么考虑的,忘记了。
2。是值字符串之间的顺序对应是不准确的。表关联是需要具体键值对应的,否则会出现交叉结果。
3。当前的结果要求只能考虑将试题id和类型重新又组织回t_record中,然后同时分隔了。
*/
--测试表及数据
create table t_TRecord(id int,qid int,accountid int,myanswer nvarchar(1000),myscore nvarchar(1000),score int,testtime datetime)
insert into t_TRecord
select 14, 17, 10, '3,2,4,1,3,2,3,2,2,3,3,3,3,2', '3,2,4,1,3,2,3,2,2,3,3,3,3,2', 36, '2002-12-28' union
select 15, 14, 1133, '1,2,2,3,4,2,3,4,3,3', '1,1,0,0,1,0,0,1,0,0' ,4, '2002-12-28' union
select 16 ,17, 1133 ,'2,2,2,2,2,2,2,2,2,2,2,2,2,2' ,'2,2,2,2,2,2,2,2,2,2,2,2,2,2', 28, '2002-12-28' union
select 17 ,14 ,1222, '1,2,2,3,4,2,3,4,3,3', '1,1,0,0,1,0,0,1,0,0' ,4, '2002-12-29'
go
create table t_TQuiz_Detail_(id int,qid int,qtype int)
insert into t_TQuiz_Detail_
select 1,14,1 union
select 2,14,1 union
select 3,14,3 union
select 4,14,2 union
select 5,14,5 union
select 6,14,4 union
select 7,14,6 union
select 8,14,8 union
select 9,14,3 union
select 10,14,6
--辅助函数取出试题id。
go
create function fns_getst(@qid int)
returns varchar(1000)
as
begin
declare @st varchar(1000)
select @st=''
select @st=@st+','+convert(varchar,id) from t_TQuiz_Detail_ where qid=@qid order by id
select @st=stuff(@st,1,1,'')
return @st
end
go
--辅助函数取出试题id。
create function fns_getclass(@qid int)
returns varchar(1000)
as
begin
declare @st varchar(1000)
select @st=''
select @st=@st+','+convert(varchar,qtype) from t_TQuiz_Detail_ where qid=@qid order by id
select @st=stuff(@st,1,1,'')
return @st
end
go--生成辅助临时表,注意数据行的多少,根据实际修改。
select top 10000 [id]=identity(int,1,1) into #temp
from (select top 100 id from syscolumns) a,
(select top 100 id from syscolumns) b
--查询出结果,此结果就是插入grade表中的记录。
select top 100 percent a.accountid as username
,stid=substring(a.stids,b.[id],charindex(',',a.stids+',',b.[id])-b.[id])
,an_id=substring(a.myanswer,b.[id],charindex(',',a.myanswer+',',b.[id])-b.[id])
,score=substring(a.myscore,b.[id],charindex(',',a.myscore+',',b.[id])-b.[id])
,class=substring(a.classids,b.[id],charindex(',',a.classids+',',b.[id])-b.[id])
,qid as item
from (select new2.accountid ,new2.qid,new2.myanswer,new2.myscore,new1.stids,new1.classids
from (
select qid,stids=dbo.fns_getst(qid),classids=dbo.fns_getclass(qid)
from t_TQuiz_Detail_
group by qid ) new1,
t_TRecord new2
where new1.qid=new2.qid) a,#temp b
where substring(','+a.myanswer,b.[id],1)=','
order by a.accountid,qid--删除临时表
drop table #temp
--删除辅助函数
drop function dbo.fns_getst
drop function dbo.fns_getclass
--删除测试表
drop table t_TRecord
drop table t_TQuiz_Detail_
/*结果
1133 1 1 1 1 14
1133 2 2 1 1 14
1133 3 2 0 3 14
1133 4 3 0 2 14
1133 5 4 1 5 14
1133 6 2 0 4 14
1133 7 3 0 6 14
1133 8 4 1 8 14
1133 9 3 0 3 14
1133 10 3 0 6 14
1222 1 1 1 1 14
1222 2 2 1 1 14
1222 3 2 0 3 14
1222 4 3 0 2 14
1222 5 4 1 5 14
1222 6 2 0 4 14
1222 7 3 0 6 14
1222 8 4 1 8 14
1222 9 3 0 3 14
1222 10 3 0 6 14*/