CREATE TABLE T82 ( ID NUMBER(4), DealDate DATE, RESULT VARCHAR2(20), Comments VARCHAR2(20) );
INSERT INTO T82 VALUES(1, to_date('2011-10-02', 'YYYY-MM-DD'), 'Y', NULL); INSERT INTO T82 VALUES(2, to_date('2011-10-02', 'YYYY-MM-DD'), 'N', 'Reject'); INSERT INTO T82 VALUES(3, to_date('2011-10-02', 'YYYY-MM-DD'), 'Y', NULL); INSERT INTO T82 VALUES(4, to_date('2011-10-02', 'YYYY-MM-DD'), 'N', 'Reject'); INSERT INTO T82 VALUES(5, to_date('2011-10-02', 'YYYY-MM-DD'), 'N', 'Reject'); INSERT INTO T82 VALUES(6, to_date('2011-10-02', 'YYYY-MM-DD'), 'Y', NULL); INSERT INTO T82 VALUES(7, to_date('2011-10-03', 'YYYY-MM-DD'), 'N', 'Reject'); INSERT INTO T82 VALUES(8, to_date('2011-10-03', 'YYYY-MM-DD'), 'Y', NULL); INSERT INTO T82 VALUES(9, to_date('2011-10-04', 'YYYY-MM-DD'), 'Y', NULL); INSERT INTO T82 VALUES(10, to_date('2011-10-05', 'YYYY-MM-DD'), 'N', 'Reject'); INSERT INTO T82 VALUES(11, to_date('2011-10-05', 'YYYY-MM-DD'), 'Y', NULL);
查询结果要求:
统计出在2011-10-02这一天里总的条目数,Y的数目,N的数目.
结果集包含的字段是:DealDate,Y_Qty,N_Qty.
select DEALDATE, count(*) as Qty, sum(decode(RESULT,'Y',1,0)) as Y_Qty, sum(decode(RESULT,'N',1,0)) as N_Qty from T82 where to_char(DEALDATE,'yyyy-mm-dd') = '2011-10-02' group by DEALDATE;