如下表T1所示
类型Type有A B C三种
状态State有a b两种T1 (为了大家看的方便我加了空格/回车)Type    State   ...
 A        a      ..     有一条记录 A        b      ..     有二条记录
 A        b      .. B        a      ..     有二条记录
 B        a      .. B        b      ..     有一条记录 C        a      ..     有一条记录 C        b      ..     有一条记录问:
我现在想统计以Type和State分组的数量,并且安如下方式排列结果
(Type作为纵轴,State作为横轴,交叉点是记录数)T2
Type      a      b
 A        1      2
 B        2      1
 C        1      1谢谢大家

解决方案 »

  1.   


    select type,state,count(*) into # from tablename
    group by type,state
    剩下的就是交叉表的问题了,搜索一个CSDN行转列。
      

  2.   

    补充:
    实际中类型数和状态数都是不固定的,只能用distinct的方式来找出所有记录,也就是说横坐标和纵坐标的元素值都是动态的如果某一个交叉点上没有记录的话添0补位
      

  3.   

    晕,先给一个固定的。
    -----------------------------------
    create table test
    (type char(1),
    state char(1))
    insert test (type,state)
    SELECT 'A','a' union all
    SELECT 'A','b' union all
    SELECT 'A','b' union all
    SELECT 'B','a' union all
    SELECT 'B','a' union all
    SELECT 'B','b' union all
    SELECT 'C','a' union all
    SELECT 'C','b' 
    --select * from testselect type,state,count(*) as TCount into # from test group by type,state
    select * from #--用于:交叉表的列数是确定的
    select type,sum(case state when 'a' then TCount else 0 end) as a,
              sum(case state when 'b' then TCount else 0 end) as b
    from # 
    group by Typedrop table test,#
      

  4.   

    LZ结帖吧,把分给我。
    -----------------------------------------
    create table test
    (type char(1),
    state char(1))
    insert test (type,state)
    SELECT 'A','a' union all
    SELECT 'A','b' union all
    SELECT 'A','b' union all
    SELECT 'B','a' union all
    SELECT 'B','a' union all
    SELECT 'B','b' union all
    SELECT 'C','a' union all
    SELECT 'C','b' 
    --select * from testselect type,state,count(*) as TCount into # from test group by type,state
    select * from #--用于:交叉表的列数是不确定的
    declare @sql varchar(8000)set @sql = 'select Type,' 
    select @sql = @sql + 'sum(case state when '''+state+''' 
                              then TCount else 0 end) as '''+state+''','
    from (select distinct state from #) as a
    select @sql = left(@sql,len(@sql)-1) + ' from # group by Type'exec(@sql)
    /*
    --用于:交叉表的列数是确定的
    select type,sum(case state when 'a' then TCount else 0 end) as a,
              sum(case state when 'b' then TCount else 0 end) as b
    from # 
    group by Type
    */drop table test,#
      

  5.   

    --------------------------------------------------------------
    您好,我们是“2006中国杰出数据库工程师评选”活动组委会。
    您的帖子已经被我们转载到本次评选官方网站的“专家在线答疑”区。
    http://www.bestdba.cn/match_discussion.aspx在那里,进入本次评选终选的30位数据库工程师将与您展开积极的互动。他们会为您的问题提供满意的答案,此外,您还可以在“专家在线答疑”区提出新的问题并参与讨论。您的帖子位于:
    http://www.bestdba.cn/match_discussion3.aspx?pointid=458&pointid2=1&pointid3=5&pcount=stc非常感谢您对本次活动的支持!
    --------------------------------------------------------------
      

  6.   

    Create Table TEST
    (Type Char(1),
     State Char(1))
    Insert TEST
    Select 'A','a' Union All
    Select 'A','b' Union All
    Select 'A','b' Union All
    Select 'B','a' Union All
    Select 'B','a' Union All
    Select 'B','b' Union All
    Select 'C','a' Union All
    Select 'C','b' 
    GO
    --State固定的情況下的
    Select 
    Type,
    SUM(Case State When 'a' Then 1 Else 0 End) As a,
    SUM(Case State When 'b' Then 1 Else 0 End) As b
    From TEST
    Group By Type
    --State不固定的情況下的
    Declare @S Varchar(8000)
    Select @S=''
    Select @S=@S+',SUM(Case State When '''+State+'''Then 1 Else 0 End) As '+State
    From TEST Group By State Order By State
    Select @S='Select Type'+@S+' From TEST Group By Type'
    EXEC(@S)
    GO
    Drop Table TEST
    GO
    --Resilt
    /*
    Type a b
    A 1 2
    B 2 1
    C 1 1
    */