类型    编号    vit次数   pit次数    ait 次数 pit      1001        2        1        1
vit      1001        2        1        1
vit      1001        2        1        1
ait      1001        2        1        1
vit      1002        1        0        0
如上表所示,查出来某一个编号对应的三种类型,分别出现的次数做一个统计,搞了一下午,么搞出来。
请大侠们帮帮忙谢谢

解决方案 »

  1.   

    http://bbs.csdn.net/topics/390451350
    http://bbs.csdn.net/topics/390451420
    这么多都是你问的,应该都会写了
      

  2.   


    类型    编号    vit次数   pit次数    ait 次数 pit      1001        2        1        1
    vit      1001        2        1        1
    vit      1001        2        1        1
    ait      1001        2        1        1
    vit      1002        1        0        0
    这个就是结果
      

  3.   

    类型    编号   
    pit      1001        
    vit      1001        
    vit      1001       
    ait      1001       
    vit      1002       
    上边是数据,然后查询结果是这个:
    类型    编号    vit次数   pit次数    ait 次数 pit      1001        2        1        1
    vit      1001        2        1        1
    vit      1001        2        1        1
    ait      1001        2        1        1
    vit      1002        1        0        0
      

  4.   

    create table tb(type varchar(10) , no varchar(10))
    insert into tb values('pit' , '1001')
    insert into tb values('vit' , '1001')
    insert into tb values('vit' , '1001')
    insert into tb values('ait' , '1001')
    insert into tb values('vit' , '1002')
      

  5.   

    那原数据呢??

    create table tb(type varchar(10) , no varchar(10))
    insert into tb values('pit' , '1001')
    insert into tb values('vit' , '1001')
    insert into tb values('vit' , '1001')
    insert into tb values('ait' , '1001')
    insert into tb values('vit' , '1002') 
      

  6.   

    让您失望了。还是没写出来头大啊
    我已经给你写了那么多列子了,你之前提的
    客户需求变来变去,我都快疯了。我发誓要好好学sql这次涨经验了,还要多谢谢你。。
      

  7.   

    参考,下几篇中了解到几种方法进行行列转换。
    http://www.cnblogs.com/insus/articles/1971446.html
      

  8.   

     SELECT  [type] ,[NO] ,num=COUNT(1) INTO #temp  FROM  TB  GROUP BY [type] ,[NO]
     
     SELECT   
    [type] ,[NO],
     pit=SUM( CASE WHEN [type]='pit' THEN num ELSE 0 END),
      ait= SUM( CASE WHEN [type]='ait' THEN num ELSE 0 END),
      vit= SUM( CASE WHEN [type]='vit' THEN num ELSE 0 END) 
        FROM  #temp GROUP BY [type] ,[NO]
      

  9.   

    这次没错,是这样的需求。 哦那静态的SQL如下:select *,
    ifnull((select count(1) from tb where type='vit' and  tb.no = a.no group by tb.type),0) as vit ,
    ifnull((select count(1) from tb where type='pit' and  tb.no = a.no group by tb.type),0) as pit ,
    ifnull((select count(1) from tb where type='ait' and  tb.no = a.no group by tb.type),0) as ait 
    from tb a
      

  10.   

    这次没错,是这样的需求。 哦那静态的SQL如下:select *,
    ifnull((select count(1) from tb where type='vit' and  tb.no = a.no group by tb.type),0) as vit ,
    ifnull((select count(1) from tb where type='pit' and  tb.no = a.no group by tb.type),0) as pit ,
    ifnull((select count(1) from tb where type='ait' and  tb.no = a.no group by tb.type),0) as ait 
    from tb a

    谢谢tashiwoweiyi兄!不胜感激!!