日期:2014-05-16 浏览次数:20871 次
create table TEST ( ID VARCHAR2(5) , MONEY NUMBER, STATE VARCHAR2(8), RIQI VARCHAR2(10), XIAOZU VARCHAR2(10) ) insert into test (ID, MONEY, STATE, RIQI, XIAOZU) values ('1', 100, '已完成', '2012-02-10', '小组1'); insert into test (ID, MONEY, STATE, RIQI, XIAOZU) values ('2', 300, '未完成', '2012-03-1', '小组1'); insert into test (ID, MONEY, STATE, RIQI, XIAOZU) values ('3', 150, '已完成', '2012-02-15', '小组1'); insert into test (ID, MONEY, STATE, RIQI, XIAOZU) values ('4', 80, '已完成', '2012-02-23', '小组2'); insert into test (ID, MONEY, STATE, RIQI, XIAOZU) values ('5', 30, '已完成', '2012-03-23', '小组2');
select XIAOZU, substr(RIQI,1,7) month , count(decode(STATE,'已完成',1,'')) count1, sum(decode(STATE,'已完成',money,0)) money1, count(decode(STATE,'未完成',1,'')) count2, sum(decode(STATE,'未完成',money,0)) money2 from test group by XIAOZU,substr(RIQI,1,7) order by XIAOZU,substr(RIQI,1,7) xiaozu month count1 money1 count2 money2 ---------------------------------------------------- 1 小组1 2012-02 2 250 0 0 2 小组1 2012-03 0 0 1 300 3 小组2 2012-02 1 80 0 0 4 小组2 2012-03 1 30 0 0