一个表A中含有如下数据:
id       bname         bs
-----------------------------------------
1        零件1         1.1
2        零件1         1.2
3        零件2         1.1
4        零件3         1.2
5        零件4         1.3
6        零件5         1.4
7        零件6         2.1
8        零件7         2.2
9        零件8         2.1
10       零件1         1.2
.......
 
问题:
现在要统计bs为1.1、1.2、1.3等等的零件数量,其它的不分组统计,如何写这个sql
结果:
bname 1.1  1.2 1.3 1.4 2.1 2.2
------------------------------
零件1   1    2   0   0   0   0
零件2   1    0   0   0   0   0 
零件3   0    1   0   0   0   0
...................--创建表格
create table test_t
(
id int,
bname  varchar2(20),
bs varchar2(10)
);
 
--插入数据
insert into test_t
select 1,'零件1','1.1' from dual
union all
select 1,'零件1','1.2' from dual
union all
select 1,'零件1','1.2' from dual
union all
select 2,'零件2','1.1' from dual
union all
select 3,'零件3','1.2' from dual
union all
select 4,'零件4','1.3' from dual
union all
select 5,'零件5','1.4' from dual
union all
select 6,'零件6','2.1' from dual
union all
select 7,'零件7','2.2' from dual
union all
select 8,'零件8','2.1' from dual
union all
select 9,'零件9','1.3' from dual
union all
select 11,'零件10','1.2' from dual;
commit;

解决方案 »

  1.   

    select distinct t.bname,
    (select count(*) from test_t where bs = '1.1' and bname = t.bname) as "1.1",
    (select count(*) from test_t where bs = '1.2' and bname = t.bname) as "1.2",
    (select count(*) from test_t where bs = '1.3' and bname = t.bname) as "1.3",
    (select count(*) from test_t where bs = '1.4' and bname = t.bname) as "1.4",
    (select count(*) from test_t where bs = '2.1' and bname = t.bname) as "2.1",
    (select count(*) from test_t where bs = '2.2' and bname = t.bname) as "2.2"
     from test_t t
     where t.bname in ('零件1','零件2','零件3')
     order by t.bname
      

  2.   

    select distinct t.bname,
    (select count(*) from test_t where bs = '1.1' and bname = t.bname) as "1.1",
    (select count(*) from test_t where bs = '1.2' and bname = t.bname) as "1.2",
    (select count(*) from test_t where bs = '1.3' and bname = t.bname) as "1.3",
    (select count(*) from test_t where bs = '1.4' and bname = t.bname) as "1.4",
    (select count(*) from test_t where bs = '2.1' and bname = t.bname) as "2.1",
    (select count(*) from test_t where bs = '2.2' and bname = t.bname) as "2.2"
     from test_t t
     order by t.bname
      

  3.   

    davinciyxw,能否有更有效率的方法?
      

  4.   

    select bname
           ,sum(case bs when '1.1' then 1 else 0 end) "1.1"
           ,sum(case bs when '1.2' then 1 else 0 end) "1.2" 
           ,sum(case bs when '1.3' then 1 else 0 end) "1.3"
           ,sum(case bs when '1.4' then 1 else 0 end) "1.4"
           ,sum(case bs when '2.1' then 1 else 0 end) "2.1"
           ,sum(case bs when '2.2' then 1 else 0 end) "2.2"
      from test_t 
     group by bname 
     order by bname;
      

  5.   

    select bname,
    decode(bs,1.1,sum_1,'')"1.1",
    decode(bs,1.2,sum_1,'')"1.2",
    decode(bs,1.3,sum_1,'')"1.3",
    decode(bs,1.4,sum_1,'')"1.4",
    decode(bs,2.1,sum_1,'')"2.1",
    decode(bs,2.2,sum_1,'')"2.2"
    from (select distinct bname,bs,sum_1 from (select bname,bs,count(bs)over(partition by bname,bs) sum_1 from test_t));
      

  6.   

    select t.bname,
           count(decode(t.bs, 1.1, 1)) "1.1",
           count(decode(t.bs, 1.2, 1)) "1.2",
           count(decode(t.bs, 1.3, 1)) "1.3",
           count(decode(t.bs, 1.4, 1)) "1.4",
           count(decode(t.bs, 2.1, 1)) "2.1",
           count(decode(t.bs, 2.2, 1)) "2.2"
      from test_t t
    group by t.bname
    order by t.bname