我有这样一个表,
dtxx
28484124224144812884
28484124224144814284
2848211424844412*882
14481184824128414224
....
dtxx(答题信息)里的每一个数字就是一个选择题的答案,这个答题信息长度是20,就是有20个小题,每个小题有四种选择1(A),2(B),4(C),8(D),我想统计出每个小题的ABCD的比率,像下面一样
题号,A,B,C,D
1,20%,30%,40%,10%
2,10%,70%,10%,10%
....
我目前是这写的:
select 
N'1' 题号,
ltrim(cast(round(sum(case when substring(omr,1,1)='1' then 1 else 0 end)*100.00/count(*),3)AS DEC(10,3)))+'%' A,
ltrim(cast(round(sum(case when substring(omr,1,1)='2' then 1 else 0 end)*100.00/count(*),3)AS DEC(10,3)))+'%' B,
ltrim(cast(round(sum(case when substring(omr,1,1)='4' then 1 else 0 end)*100.00/count(*),3)AS DEC(10,3)))+'%' C,
ltrim(cast(round(sum(case when substring(omr,1,1)='8' then 1 else 0 end)*100.00/count(*),3)AS DEC(10,3)))+'%' D
from table1 union all 
select
N'2' 题号,
ltrim(cast(round(sum(case when substring(omr,2,1)='1' then 1 else 0 end)*100.00/count(*),3)AS DEC(10,3)))+'%' A,
ltrim(cast(round(sum(case when substring(omr,2,1)='2' then 1 else 0 end)*100.00/count(*),3)AS DEC(10,3)))+'%' B,
ltrim(cast(round(sum(case when substring(omr,2,1)='4' then 1 else 0 end)*100.00/count(*),3)AS DEC(10,3)))+'%' C,
ltrim(cast(round(sum(case when substring(omr,2,1)='8' then 1 else 0 end)*100.00/count(*),3)AS DEC(10,3)))+'%' D
from table1 union all
....
一直到20题,我要重复20次这样的语句,题少没关系,可我现在有120个题,我要写120次,我想请教大家没有更简便的方法来实现,比如说用循环语句,通过判断dtxx字段的长度来确定多少个题目,再循环多少次就得到那样的结果了,有哪位高手可以帮我解决这个问题,不胜感激!

解决方案 »

  1.   

    select rtrim((len(replace(dtxx,1,''))*100./20))+'%' as A,
           rtrim((len(replace(dtxx,2,''))*100./20))+'%' as B,
           rtrim((len(replace(dtxx,3,''))*100./20))+'%' as C,
           rtrim((len(replace(dtxx,8,''))*100./20))+'%' as D
    from tb
      

  2.   

    rtrim((len(replace(dtxx,'1',''))*100./20))+'%' as A
      

  3.   


    create table #tb(dtxx nvarchar(2000))
    insert #tb
    select '28484124224144812884' union all
    select '28484124224144814284' union all
    select '28421241821848218212' union all
    select '44481184824128414224' union all
    select '88484124224144812884' union all
    select '28484124224144814284' union all
    select '28482114248444122882' union all
    select '14481184824128414224' union all
    select '28484124224144812884' union all
    select '28484124224144814284' union all
    select '28482114248444122882' union all
    select '44481184824128414224' union all
    select '18484124224144812884' union all
    select '28484124224144814284' union all
    select '28482114248444122882' union all
    select '14481184824128414224' union all
    select '28484124224144812884' union all
    select '88484124224144814284' union all
    select '28482114248444122882' union all
    select '14481184824128414224' declare @i as int
    declare @coutn as int
    declare @sql as varchar(8000)
    declare @sql2 as varchar(8000)
    declare @sql3 as varchar(8000)
    select @coutn=count(1) from #tb
    set @sql=''
    set @sql2=''
    set @sql3=''
    set @i=1
    while @i<=@coutn
    begin
       set @sql=@sql+',o'+ltrim(@i)+' int'
       set @sql2=@sql2+',substring(dtxx,'+ltrim(@i)+',1)'
       set @sql3=@sql3+' union all select '+ltrim(@i)+' as 题号,ltrim(cast((select count(1) from @T where o'+ltrim(@i)+'=1)*100.00/'+ltrim(@coutn)+' as dec(10,3)))+''%'' as A'
                                          + ',ltrim(cast((select count(1) from @T where o'+ltrim(@i)+'=2)*100.00/'+ltrim(@coutn)+' as dec(10,3)))+''%'' as B'
                                          + ',ltrim(cast((select count(1) from @T where o'+ltrim(@i)+'=4)*100.00/'+ltrim(@coutn)+' as dec(10,3)))+''%'' as C'
                                          + ',ltrim(cast((select count(1) from @T where o'+ltrim(@i)+'=8)*100.00/'+ltrim(@coutn)+' as dec(10,3)))+''%'' as D'
       set @i=@i+1
    end
    if len(@sql)>0
    begin
       set @sql='declare @T table('+stuff(@sql,1,1,'')+')'+' insert @T select '+stuff(@sql2,1,1,'')+' from #tb'
               +stuff(@sql3,1,10,'') 
    endexec(@sql)
      

  4.   

    谢谢liang145,我试了一下,结果是我想到的结果,只是那个循环次数不是根据字段dtxx长度定义的,不过做到这样子,很不错了!
      

  5.   


    create table #tb(dtxx nvarchar(2000))
    insert #tb
    select '28484124224144812884' union all
    select '28484124224144814284' union all
    select '28421241821848218212' union all
    select '44481184824128414224' union all
    select '88484124224144812884' union all
    select '28484124224144814284' union all
    select '28482114248444122882' union all
    select '14481184824128414224' union all
    select '28484124224144812884' union all
    select '28484124224144814284' union all
    select '28482114248444122882' union all
    select '44481184824128414224' union all
    select '18484124224144812884' union all
    select '28484124224144814284' union all
    select '28482114248444122882' union all
    select '14481184824128414224' union all
    select '28484124224144812884' union all
    select '88484124224144814284' union all
    select '28482114248444122882' union all
    select '14481184824128414224' declare @T as table(id int identity(1,1),answer varchar(max))  --这种做法,#tb里的记录不能超过8000条,如果超中,考虑用text
    declare @answer as varchar(max)
    declare @count as int
    declare @len as int
    declare @i as int
    select @count=count(1) from #tb
    select top 1 @len=len(dtxx) from #tb
    set @i=1
    set @answer=''
    while @i<=@len
    begin
      select @answer=@answer+substring(dtxx,@i,1) from #tb
      insert @T(answer) values (@answer)
      set @answer=''
      set @i=@i+1
    end
    select id as 题号,
    ltrim(cast((len(replace(answer,'1',',,'))-len(answer))*100.00/@count as dec(10,3)))+'%' as A,
    ltrim(cast((len(replace(answer,'2',',,'))-len(answer))*100.00/@count as dec(10,3)))+'%' as B,
    ltrim(cast((len(replace(answer,'4',',,'))-len(answer))*100.00/@count as dec(10,3)))+'%' as C,
    ltrim(cast((len(replace(answer,'8',',,'))-len(answer))*100.00/@count as dec(10,3)))+'%' as D
    from @T