模拟数据卡号                       金额                      缴费类型
cardasn          amount                    type
001                        10                        1
001                        40                        1
001                        50                        2
001                        60                        3
002                        10                        2
002                        60                        2
002                        90                        4期望得到的查询结果是:(缴费类型只有4个)cardasn        type1_amount          type2_amount        type3_amount       type4_amount
001            50                     50                  60                  0
002            0                      70                  0                   90按卡号分大类,然后按缴费类型分小类统计出金额来 请问应该如何实现啊????

解决方案 »

  1.   

    select cardasn,
           nvl(sum(decode(type,1,amount)),0) type1_amount,
           nvl(sum(decode(type,2,amount)),0) type2_amount,
           nvl(sum(decode(type,3,amount)),0) type3_amount,
           nvl(sum(decode(type,4,amount)),0) type4_amount
    from tt
    group by cardasn       
      

  2.   

    结果的数据有点不对:
    卡号                      金额                      缴费类型 
    cardasn          amount                    type 
    001                        10                        1 
    001                        40                        1 
    001                        50                        2 
    001                        60                        3 
    002                        10                        2 ----
    002                        60                        2   ---
    三行红色?
    002                        90                        4 
      

  3.   

    cardasn        type1_amount          type2_amount        type3_amount      type4_amount 
    001            50                    50                  60                  0 
    002            0                      70                 60                  90 
      

  4.   

    数据:
             cardasn type                            amount
    1 1 1001 1 2009-08-01 ssssss 100000 100101
    2 2 1001 1 2009-08-02 aaaaa 20000 100101
    3 3 1001 2 2009-08-03 bbbbb 45000 100102
    4 4 1001 3 2009-08-04 ccccc 580100 100103
    5 5 1002 1 2009-08-05 ddddd 49855 100201
    6 6 1002 3 2009-08-06 eeeee 98200 100203
    7 7 1003 2 2009-08-07 ffffff 100 100302
    8 8 1003 2 2009-08-08 ggggg 2500 100302
    9 9 1003 3 2009-08-09 hhhhh 8510 100303
    10 10 1003 5 2009-08-10 iiiii 6200 100305
    11 11 1003 6 2009-08-11 jjjjj 8520 100306
    12 12 1003 11 2009-08-12 kkkkk 6520 100311结果:
    1 1002 49855 0 98200 0 0 0
    2 1001 120000 45000 580100 0 0 0
    3 1003 0 2600 8510 6200 8520 6520