把myanswer中的第一项取出来插到grade中作为stid???stid不是试题题号,对应t_TQuiz_Detail表中id吗?  

解决方案 »

  1.   

    /*一条语句显然是无法完成的。给楼主一段示例代码。1.楼主描述的有些对应关系不是很好,无法具体写出。2。t_TRecord表中是答题的汇总信息。那么再回去找出原题的id给grade的stid,难以准确对应。3。楼主根据已分解出记录的查询结果,自己来组织实现吧。*/
    --测试表及数据
    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
    /*结果
    自己运行查看。
    */
      

  2.   

    TO rivery:
    我关于什么对应关系的描述不清楚?你说的第二个问题其实不难啊,因为它的myanswer的值里第一个数就表示第一道题,第二个就表示第二道,中间用逗号隔开的嘛!
      

  3.   

    拿1,2,2,3,4,2,3,4,3,3来说明,其实它说明了两个情况,一是本期题有10道,二是从1到10题各自选择的答案为每道题对应选项中的第1、第2、第2、第3、第4、第2、第3、第4、第3、第3,既然知道了题号,那么这题对应的类型也就能取出来加到grade表里去了吧?你给我的那些代码没有问题,现在就是如何完整的取出各答案所对应的题的类型也加到grade表中去。
      

  4.   

    "因为它的myanswer的值里第一个数就表示第一道题,第二个就表示第二道,中间用逗号隔开的嘛"也就是说grade的stid的值是算出来的,而不是一开始你说的"把myanswer中的第一项取出来插到grade中作为stid"如何完整的取出各答案所对应的题的类型也加到grade表中去可以在后面加一条更新语句update grade 
    set class = t2.Qtype
    from grade t1,t_TQuiz_Detail t2
    where t1.item = t2.qid and t1.stid = t2.id
      

  5.   

    /*
    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*/