-- create sample table
create table tab1 (id int, output_style char(1), amount int, output_date date)
insert into tab1 values(1,'D',1000,to_date('2009-11-12','yyyy-mm-dd'))
insert into tab1 values(2,'C',1000,to_date('2009-11-12','yyyy-mm-dd'))
insert into tab1 values(3,'G',1000,to_date('2009-12-12','yyyy-mm-dd'))
insert into tab1 values(4,'Z',1000,to_date('2010-01-01','yyyy-mm-dd'))
insert into tab1 values(5,'D',1300,to_date('2009-11-12','yyyy-mm-dd'))
insert into tab1 values(6,'C',1400,to_date('2009-11-12','yyyy-mm-dd'))
insert into tab1 values(7,'G',2000,to_date('2009-12-12','yyyy-mm-dd'))
insert into tab1 values(8,'Z',1000,to_date('2010-01-01','yyyy-mm-dd'))
-- query
select substr(to_char(output_date,'yyyy-mm-dd'),1,7) as yearmonth,
sum(case when output_style='C' then amount else 0 end) as C_Amount,
sum(case when output_style='G' then amount else 0 end) as G_Amount,
sum(case when output_style='Z' then amount else 0 end) as Z_Amount,
sum(case when output_style='D' then amount else 0 end) as D_Amount from tab1 group by output_date
这个用单一sql还真难写,用存储过程还行。楼主你的需求只与GID和DateAdd有关,可以用游标把这两个字段的内容保存下来。再通过类似,select * from xx group by gid. 再进一步分析。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)