地区 序号 金额(这个是随机的合计数)
a 1 8
a 2 8.5
b 3 18
b 4 20
c 5 50
c 6 80
d 7 2
d 8 3
d 9 6
要求得到如下结果:
0-10 10-30 30-50 50-100 100以上
a 2 0 0 0 0
b 0 2 0 0 0
c 0 0 1 1 0
d 3 0 0 0 0
大概就是这个意思,不过实际里行很多,主要是想知道 SQL语句里如何实现这个数据分组的。
谢谢.

解决方案 »

  1.   

    看起来不错嘛:
    SQL> select t.* from t_test t;COL1             COL2
    ---------- ----------
    a                   8
    a                 8.5
    b                  18
    b                  20
    c                  50
    c                  80
    d                   2
    d                   3
    d                   69 rows selectedSQL> select a.col1
      2         , sum(case when a.col2 <= 10 then 1 else 0 end) as "0-10"
      3         , sum(case when a.col2 > 10 and a.col2 <= 30 then 1 else 0 end) as "10-30"
      4         , sum(case when a.col2 > 30 and a.col2 <= 50 then 1 else 0 end) as "30-50"
      5         , sum(case when a.col2 > 50 and a.col2 <= 100 then 1 else 0 end) as "50-100"
      6         , sum(case when a.col2 > 100 then 1 else 0 end) as "100以上"
      7    from t_test a
      8   group by a.col1;COL1             0-10      10-30      30-50     50-100    100以上
    ---------- ---------- ---------- ---------- ---------- ----------
    a                   2          0          0          0          0
    b                   0          2          0          0          0
    c                   0          0          1          1          0
    d                   3          0          0          0          0SQL>