如何使用分组查询

时间:2012-05-31 04:16:08

标签: oracle

嗨请帮我解决一下我的疑问,我在oracle9i中获得了这样的输出,

S.No   Column1     Column2  Column3  DateCol

1      A            B        C        10/2001 

2      A            B        C        03/2001  

3      B            B        C        02/2001 

4      B            B        C        01/2001

5      A            B        C        03/2000   

但我真实的情况是,我需要在下面的结构中填充输出

S.No   Column1     Column2  Column3  DateCol

1      A            B        C        10/2001 

       A            B        C        03/2001  

2      B            B        C        02/2001 

       B            B        C        01/2001

3      A            B        C        03/2000    

我不知道如何形成查询,检索结构,请帮帮我,我的学术项目已经过时了,请有人为我提供解决方案。谢谢提前

1 个答案:

答案 0 :(得分:0)

CREATE TABLE SCOTT.GROUP_SHOW_ONCE
(
  SR_NO    NUMBER,
  COL1     VARCHAR2(20 BYTE),
  COL2     VARCHAR2(20 BYTE),
  COL3     VARCHAR2(20 BYTE),
  DATECOL  DATE
)

Insert into GROUP_SHOW_ONCE
   (SR_NO, COL1, COL2, COL3, DATECOL)
 Values
   (1, 'A', 'B', 'C', TO_DATE('10/11/2012 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
Insert into GROUP_SHOW_ONCE
   (SR_NO, COL1, COL2, COL3, DATECOL)
 Values
   (2, 'A', 'B', 'C', TO_DATE('03/11/2012 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
Insert into GROUP_SHOW_ONCE
   (SR_NO, COL1, COL2, COL3, DATECOL)
 Values
   (3, 'B', 'B', 'C', TO_DATE('02/11/2012 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
Insert into GROUP_SHOW_ONCE
   (SR_NO, COL1, COL2, COL3, DATECOL)
 Values
   (4, 'B', 'B', 'C', TO_DATE('01/11/2012 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
Insert into GROUP_SHOW_ONCE
   (SR_NO, COL1, COL2, COL3, DATECOL)
 Values
   (5, 'A', 'B', 'C', TO_DATE('03/11/2011 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
COMMIT;

---你的查询会喜欢

SELECT DECODE (rnn, LAG (rnn) OVER (ORDER BY TO_NUMBER (rnn)), NULL, rnn) sr_no,col1, col2, col3,datecol
  FROM (SELECT   col1, col2, col3,DATECOL,
                 ROW_NUMBER () OVER (PARTITION BY rn ORDER BY rn) rnn
            FROM (SELECT sr_no, col1, col2, col3,DATECOL,
                         ROW_NUMBER () OVER (PARTITION BY col1, col2, col3 ORDER BY sr_no)  rn
                    FROM group_show_once)
        ORDER BY rnn)