解决方案 »

  1.   

    Model_No这个就是型号呀。
    只有一个表。这个机试的一个题目。
      

  2.   

    只有六个月的话可以直接先UNION ALL
    然后再行转列啊。
      

  3.   

    select 大类,小类,型号,'1' 月份,
    sum(case when BizType='p' then Month1Num else 0 end) as p,
    sum(case when BizType='s' then Month1Num else 0 end) as s,
    sum(case when BizType='i' then Month1Num else 0 end) as i
    from  表  group by 大类,小类,型号
    union all
    select 大类,小类,型号,'2' 月份,
    sum(case when BizType='p' then Month2Num else 0 end) as p,
    sum(case when BizType='s' then Month2Num else 0 end) as s,
    sum(case when BizType='i' then Month2Num else 0 end) as i
    from  表  group by 大类,小类,型号
    union all
    select 大类,小类,型号,'3' 月份,
    sum(case when BizType='p' then Month3Num else 0 end) as p,
    sum(case when BizType='s' then Month3Num else 0 end) as s,
    sum(case when BizType='i' then Month3Num else 0 end) as i
    from  表  group by 大类,小类,型号
    .... 有几个月union 前提 一个大类,小类,型号  在表中 只用 p s  i 3条记录
      

  4.   


    select
    BigCategory,SmallCategory,Model_No ,Month
    max(case BizType when 'p'  then Month1Num else 0 end) as 'P',
    max(case BizType when 'S'  then Month1Num else 0 end) as 'S',
    max(case BizType when 'I'  then Month1Num else 0 end) as 'I',
    from
    (
    select BigCategory,SmallCategory,Model_No ,BizType,Month1Num,1 as [Month] from tb
    union all
    select BigCategory,SmallCategory,Model_No ,BizType,Month2Num,2 as [Month] from tb
    union all
    select BigCategory,SmallCategory,Model_No ,BizType,Month3Num,3 as [Month] from tb
    union all
    select BigCategory,SmallCategory,Model_No ,BizType,Month4Num,4 as [Month] from tb
    union all
    select BigCategory,SmallCategory,Model_No ,BizType,Month5Num,5 as [Month] from tb
    union all
    select BigCategory,SmallCategory,Model_No ,BizType,Month6Num,6 as [Month] from tb
    )t
    group by
      BigCategory,SmallCategory,Model_No ,Month盘点不知道怎么算的 自己去算下
      

  5.   

    select
    BigCategory,SmallCategory,Model_No ,Month
    sum(case BizType when 'p'  then Month1Num else 0 end) as 'P',
    sum(case BizType when 'S'  then Month1Num else 0 end) as 'S',
    sum(case BizType when 'I'  then Month1Num else 0 end) as 'I',
    from
    (
    select BigCategory,SmallCategory,Model_No ,BizType,Month1Num,1 as [Month] from tb
    union all
    select BigCategory,SmallCategory,Model_No ,BizType,Month2Num,2 as [Month] from tb
    union all
    select BigCategory,SmallCategory,Model_No ,BizType,Month3Num,3 as [Month] from tb
    union all
    select BigCategory,SmallCategory,Model_No ,BizType,Month4Num,4 as [Month] from tb
    union all
    select BigCategory,SmallCategory,Model_No ,BizType,Month5Num,5 as [Month] from tb
    union all
    select BigCategory,SmallCategory,Model_No ,BizType,Month6Num,6 as [Month] from tb
    )t
    group by
      BigCategory,SmallCategory,Model_No ,Month应该是SUM
      

  6.   

    先将列转行
    变成每个月多少量,然后在行转列,但BizType进行行转列
      

  7.   

    根据你的方法确实可行,效果如下:这样查出来是IPHONE5的数据,我想要得到的是所有的数据,小弟是菜鸟。还望赐教
      

  8.   

    亲 你的这种方法不行啊  是要用一条SQL  你这都好多条了
      

  9.   

    根据你的方法确实可行,效果如下:这样查出来是IPHONE5的数据,我想要得到的是所有的数据,小弟是菜鸟。还望赐教
    哎哟,还发现一个问题用SUM的话得到的是总的数量,用MAX是得到最大数量。这样结果就不对呀
      

  10.   

    根据你的方法确实可行,效果如下:这样查出来是IPHONE5的数据,我想要得到的是所有的数据,小弟是菜鸟。还望赐教
    哎哟,还发现一个问题用SUM的话得到的是总的数量,用MAX是得到最大数量。这样结果就不对呀用sum是求和吧,总的数量,那么会有什么问题呢
      

  11.   

    根据你的方法确实可行,效果如下:哎哟,还发现一个问题用SUM的话得到的是总的数量,用MAX是得到最大数量。这样结果就不对呀用sum是求和吧,总的数量,那么会有什么问题呢
    我要的不是求和呀。是每个月份对应的 P S I  而不是总的P S I
      

  12.   

    根据你的方法确实可行,效果如下:哎哟,还发现一个问题用SUM的话得到的是总的数量,用MAX是得到最大数量。这样结果就不对呀用sum是求和吧,总的数量,那么会有什么问题呢
    我要的不是求和呀。是每个月份对应的 P S I  而不是总的P S I哦,会不会,在一个月同一个大类、小类、款式,有2个P呢
      

  13.   


    select BigCategory,SmallCategory,Model_No,month,P=sum(case when BizType='P' then Month1Num else 0 end),
    S=sum(case when BizType='S' then Month1Num else 0 end),
    I=sum(case when BizType='I' then Month1Num else 0 end),
    盘点=sum(case when BizType='P' then Month1Num else 0 end-case when BizType='S' then Month1Num else 0 end-case when BizType='I' then Month1Num else 0 end)
    from 
    (
    select BigCategory,SmallCategory,Model_No,BizType,1 as month,Month1Num from Phone
    union all select BigCategory,SmallCategory,Model_No,BizType,2,Month2Num from Phone
    union all select BigCategory,SmallCategory,Model_No,BizType,3,Month3Num from Phone
    union all select BigCategory,SmallCategory,Model_No,BizType,4,Month4Num from Phone
    union all select BigCategory,SmallCategory,Model_No,BizType,5,Month5Num from Phone
    union all select BigCategory,SmallCategory,Model_No,BizType,6,Month6Num from Phone
    )t
    group by BigCategory,SmallCategory,Model_No,month
    order by BigCategory
    /*
    BigCategory    SmallCategory    Model_No    month   P   S   I   盘点
    -------------------------------------------
    电脑 联想 V480 1 21 0 0 21
    电脑 联想 V480 2 30 0 0 30
    电脑 联想 V480 3 30 0 0 30
    电脑 联想 V480 4 30 0 0 30
    电脑 联想 V480 5 30 0 0 30
    电脑 联想 V480 6 30 0 0 30
    电脑 联想 Z370 1 20 0 0 20
    电脑 联想 Z370 2 20 0 0 20
    电脑 联想 Z370 3 20 0 0 20
    电脑 联想 Z370 4 20 0 0 20
    电脑 联想 Z370 5 21 0 0 21
    电脑 联想 Z370 6 20 0 0 20
    手机 HTC BUTTERFLY 1 10 1 8 1
    手机 HTC BUTTERFLY 2 10 1 9 0
    手机 HTC BUTTERFLY 3 10 1 9 0
    手机 HTC BUTTERFLY 4 10 2 9 -1
    手机 HTC BUTTERFLY 5 10 1 9 0
    手机 HTC BUTTERFLY 6 10 1 9 0
    手机 苹果 IPHONE4S 1 10 10 0 0
    手机 苹果 IPHONE4S 2 10 10 0 0
    手机 苹果 IPHONE4S 3 10 10 0 0
    手机 苹果 IPHONE4S 4 10 10 0 0
    手机 苹果 IPHONE4S 5 10 10 0 0
    手机 苹果 IPHONE4S 6 10 10 0 0
    手机 苹果 IPHONE5 1 10 5 5 0
    手机 苹果 IPHONE5 2 10 4 6 0
    手机 苹果 IPHONE5 3 10 3 7 0
    手机 苹果 IPHONE5 4 10 7 3 0
    手机 苹果 IPHONE5 5 10 2 8 0
    手机 苹果 IPHONE5 6 10 9 1 0
    */