Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: SQL Question
CREATE TABLE SCHEDULE(SITE_ID NUMBER, TYPE CHAR(1));
INSERT INTO SCHEDULE VALUES(101, 'A'); INSERT INTO SCHEDULE VALUES(101, 'B'); INSERT INTO SCHEDULE VALUES(101, 'B'); INSERT INTO SCHEDULE VALUES(102, 'B'); INSERT INTO SCHEDULE VALUES(102, 'C');
INSERT INTO OBJECTS VALUES(1, 101); INSERT INTO OBJECTS VALUES(3, 101); INSERT INTO OBJECTS VALUES(4, 101); INSERT INTO OBJECTS VALUES(2, 102); INSERT INTO OBJECTS VALUES(5, 102);
SELECT OBJECTS_TE.OBJECT_ID, SCHEDULE_TE.SITE_ID, SCHEDULE_TE.TYPE
FROM
(SELECT SITE_ID, TYPE, ROW_NUMBER() OVER(PARTITION BY SITE_ID ORDER BY
TYPE) RN FROM SCHEDULE) SCHEDULE_TE
JOIN
(SELECT OBJECT_ID, SITE_ID, ROW_NUMBER() OVER(PARTITION BY SITE_ID
ORDER BY OBJECT_ID) RN FROM OBJECTS) OBJECTS_TE
ON SCHEDULE_TE.SITE_ID = OBJECTS_TE.SITE_ID
AND SCHEDULE_TE.RN = OBJECTS_TE.RN;
OBJECT_ID SITE_ID TYPE
---------- ---------- ----
1 101 A 3 101 B 4 101 B 2 102 B 5 102 C
DROP TABLE SCHEDULE;
DROP TABLE OBJECTS;
Received on Tue Dec 21 2004 - 15:20:13 CST