SQL:行列转换

简单的列子:

建表:
create table t_result

(d date,result varchar2(4));

 

插入数据:
insert into t_result values (date '2014-01-01','胜');

insert into t_result values (date '2014-01-01','胜');

insert into t_result values (date '2014-01-01','负');

insert into t_result values (date '2014-01-02','胜');

insert into t_result values (date '2014-01-02','负');

insert into t_result values (date '2014-01-02','负');

 

decode

SELECT d,SUM(decode(result,'胜',1,0)),SUM(decode(result,'负',1,0))
FROM t_result
GROUP BY d

 

case when

select d,
sum(case result when '胜' then 1 else 0 end )胜,

sum(case result when '负' then 1else 0 end )负

from t_result group by d order by d;

 

privot

select * from t_result pivot(count(*) forresult in('胜' 胜,'负' 负));


还有比方sys_connect_by_path


你可能感兴趣的:(SQL:行列转换)