表T,3个字段pk,a,b。pk是自增列,a有1234这4种取值,b是int类型。现在要查出这么一种东西来:        1           2           3           4
0       ?          ×          @           %
1       &           
其中?是T.a=1,T.b=0的记录条数,×是T.a=2,T.b=0的记录条数,@是T.a=3,T.b=0的记录条数&是T.a=1,T.b=1的记录条数以此类推。因为a的取值只有4种,所以按1234分别写4个存储过程,然后用并排的4个Datalist来显示,在具体实际情况中是可接受的,而暂时我也是这么考虑的,不过希望大家是否帮忙再想想办法。
顶有分,解决加到200,不够可以再开贴再加200

解决方案 »

  1.   

    采用case
    select pk, case a when 1 then b else '' end as '1', case a when 2 then b else '' end as '2', case a when 3 then b else '' end as '3', case a when 4 then b else '' end as '4'
    from T
      

  2.   

    可以变通一下。
    如:
    select AA.a1, BB.a2, CC.a3, DD.a4
    from (select b, count(*) as a1 from T where a=1 group by b) AA
    Full ouuer join (select b, count(*) as a2 from T where a=2 group by b) BB
    on AA.b=BB.b
    Full ouuer join (select b, count(*) as a3 from T where a=3 group by b) CC
    on CC.b=BB.b
    Full ouuer join (select b, count(*) as a4 from T where a=4 group by b) DD
    on CC.b=DD.b
      

  3.   

    现在主要问题是用“可接受”的sql语句(或存储过程)把东西查出来。用Datalist,Datagrid,Asp:Table,Repeater这些都没什么关系。
    我说的“可接受”,比如下面这种方法就是“不可接受”的:
    先用查询查出到底这个东西有多少行(假设n),然后用n×4条Sql语句来分别填充这4n个td。
    因为n到底是多少根本无法预测,有可能需要几百上千条语句。按列分开是可接受的,因为我们已经知道是4种情况。
      

  4.   

    楼柱说的是作交叉表吧?不知你使用的什么数据库。像 Access 和 Oracle 都是可以做交叉表的。Access可以用向导来建,而Oracle可以用decode函数。
      

  5.   

    cnming(cnming) ( ) 信誉:98  2005-04-12 08:37:00  得分: 0  
     
     
       采用case
    select pk, case a when 1 then b else '' end as '1', case a when 2 then b else '' end as '2', case a when 3 then b else '' end as '3', case a when 4 then b else '' end as '4'
    from T
      
     
      

  6.   

    阿豹的方法查出来的结果是正确的,谢谢,马上揭帖,也谢谢cnming
      

  7.   

    呵呵,刚才CSDN出问题了,我给分失败了,所以你没来迟。阿豹100,其他顶的1人5分,剩下全给cnming。
      

  8.   

    select pk, case a when 1 then b else '' end as '1', case a when 2 then b else '' end as '2', case a when 3 then b else '' end as '3', case a when 4 then b else '' end as '4'
    from T
      

  9.   

    n=select max(b) from T
    for(i=1;i<=4;i++)
       for(j=0;j<=n;j++)
         list_i.value=select count(*) from T where a=i and b=j