日期:2014-05-17  浏览次数:21026 次

急啊,初学者,搞了2天求一条sql语句
我有个统计比较复杂,有个关键,如下

表A 有 2个字段
  字段一 字段二 
  a t
  a t
  a t
  a t
  a w
  a w
  a w
  b w
  c t
  c t
   
   
  用一条sql 查询结果如下
  字段1 字段二 字段三 字段四 字段五
  a t 4 w 2 eg:即 统计t出现了4次,w出现了2次
  b t 0 w 1
  c t 2 w 0

  先谢谢一下

------解决方案--------------------
select 字段一,'t',sum(decode(字段二,'t',1,0)) t数目,'w',sum(decode(字段二,'w',1,0)) w数目 from 表名 group by 字段一;
------解决方案--------------------
SQL code

create table t1 (c1 varchar2(5),c2 varchar2(5));
insert into t1 values ('a','t');
insert into t1 values ('a','t');
insert into t1 values ('a','t');
insert into t1 values ('a','t');
insert into t1 values ('a','w');
insert into t1 values ('a','w');
insert into t1 values ('a','w');
insert into t1 values ('b','w');
insert into t1 values ('c','t');
insert into t1 values ('c','t');
insert into t1 values ('c','w');
commit;

select c1,
       't' t,
       sum(decode(c2,'t',1,0)) t_ct,
       'w' w,
       sum(decode(c2,'w',1,0)) w_ct
from t1
group by c1

     c1    t    t_ct    w    w_ct
-----------------------------------------------
1    a    t    4    w    3
2    b    t    0    w    1
3    c    t    2    w    1

------解决方案--------------------
SQL code

select c1,wmsys.wm_concat(cnt) cnt from(
select c1,c2||'_'||max(rn) cnt from (
select t.c1,t.c2,count(1) over(partition by c1,c2 order by t.c1) rn  from t1 t
 )tab 
group by c1,c2 ) tab1
group by c1