表:card_no   money
11111      90
22222      110
11111      80
33333      3000
33333      12000
44444      4000
55555      600000
统计如下金额         消费卡的数   消费记录数   总金额
100以下      1            2        170
100-1000     1            1        110
1000-10000   2            2        7000
10000以上      2            2        612000合计            6            7        619280

解决方案 »

  1.   

    合计可以就不用SQL显示了~ 
      

  2.   

    select 金额,count(distinct card_no) 消费卡的数,count(1)  消费记录数,sum(money) 总金额 
    from (
    select  case when money < 100 then '100以下'
                 when money < 1000 and money >100 then '100-1000
                 when money < 10000 and money > 1000 then '1000-10000'
                 when money > 10000 then '10000以上' 金额 ,card_no,money 
     from table_name

    group by 金额;
      

  3.   

    select 金额,count(distinct card_no) 消费卡的数,count(1) 消费记录数,sum(money) 总金额 from(select (case when money <100 then '100以下' when money <1000 and money > 100 then '100-1000' when money <10000 and money >1000 then '1000-10000' when money >10000 then '10000以上' else 'no data' end) 金额,card_no,money from xiaofei)group by 金额;
    楼上那人 没写END  呵呵
      

  4.   

    如果加多一个表是卡的类型表
    cardType_table
    01    VIP卡
    02    信用卡
    03    储蓄卡
    交易表:
    TCC_TABLE
    card_no         money       type
    11111             90         01
    22222             110        02
    11111             80         03
    33333             3000       02
    33333             12000      01
    44444             4000       03
    55555             600000     01
    统计如下 卡类型 金额                   消费卡的数       消费记录数       总金额 
    VIP卡  100以下                1                     2          170 
          100-1000              1                   1           110 
          1000-10000          2                     2           7000 
          10000以上                2                    2          612000 信用卡  100以下                1                     2          170 
          100-1000              1                   1           110 
          1000-10000          2                     2           7000 
          10000以上                2                    2          612000 ......合计                         12                   14         ......