2017-09-12 63 views
0

基本上,我想顯示在同一個課程中有多個註冊的用戶。當我嘗試時,我沒有成功,因爲這比我預期的更復雜,並且我有一個不那麼簡單的情況(我嘗試應用類似的案例)。如何只顯示一個字段有多個主題的行

我需要顯示這些行基於其他表,即與JOINs。

下面的查詢只顯示了需要使用此操作處理的三列。如果可能的話,這個查詢應該是其他JOIN的一部分,因爲我需要顯示關於類的其他細節。

SELECT eseat.USER_ID, c.COURSE_ID, t.CLASS_ID 
FROM 
    table_CLASS t                  -- Classes 
    LEFT JOIN                   -- This JOIN is necessary to have only the future Classes 
    (
     SELECT aa.CLASS_ID, MIN(aa.START_DATE) AS "START", MAX(aa.END_DATE) AS "END" 
     FROM table_SEG_CLASS aa               -- Segment x Class 
     GROUP BY aa.CLASS_ID 
    ) seg ON seg.CLASS_ID = t.CLASS_ID 
    LEFT JOIN table_ENROLL_CLASS eseat ON eseat.CLASS_ID = t.CLASS_ID     -- Enrollment x Class 
    LEFT JOIN table_ENROLL_STAT estat ON estat.ENRL_STAT_ID = eseat.ENRL_STAT_ID  -- Enrollment status type 
    LEFT JOIN table_COURSE c ON c.COURSE_ID = t.COURSE_ID        -- Course x Class 

WHERE 
    1=1 
    AND t.CANCELLED = 'N'    -- Only active classes 
    AND estat.ENRL_STAT_TYP_ID <> 'C' -- Not show cancelled enrollment status 
    AND SYSDATE <= "END"    -- To show only future classes 

貝婁,這些表的領域。

[table_CLASS]

CLASS_ID | COURSE_ID 
T_0001 | C_0001 
T_0002 | C_0001 
T_0101 | C_0002 
T_0201 | C_0003 
T_0301 | C_0004 
... 

[table_SEG_CLASS]

CLASS_ID | START_DATE | END_DATE 
T_0001 | 2019-01-01 | 2019-01-02 
T_0002 | 2019-02-01 | 2019-02-02 
T_0101 | 2019-03-01 | 2019-03-02 
T_0201 | 2019-04-01 | 2019-04-02 
T_0301 | 2019-05-01 | 2019-05-02 
... 

[table_ENROLL_CLASS]

USER_ID | CLASS_ID | ENRL_STAT_ID 
00001 | T_0001 | ENROLL 
00001 | T_0002 | ENROLL 
00001 | T_0101 | ENROLL 
00001 | T_0201 | ENROLL 
00001 | T_0301 | ENROLL 
00002 | T_0001 | ENROLL 
00002 | T_0101 | ENROLL 
00002 | T_0201 | ENROLL 
00002 | T_0301 | ENROLL 
... 

[table_ENROLL_STAT]

ENRL_STAT_ID | ENRL_STAT_TYP_ID 
ENROLL  | E 
CANCELLED | C 
WAITLIST  | W 
... 

[table_COURSE]

COURSE_ID | COURSE_NAME 
C_0001 | Course 01 
C_0002 | Course 02 
C_0003 | Course 03 
C_0004 | Course 04 
... 

OUTPUT

USER_ID | COURSE_ID | CLASS_ID 
00001 | C_0001 | T_0001 
00001 | C_0001 | T_0002 
00001 | C_0002 | T_0101 
00001 | C_0003 | T_0201 
00001 | C_0004 | T_0301 
00002 | C_0001 | T_0001 
00002 | C_0002 | T_0101 
00002 | C_0003 | T_0201 
00002 | C_0004 | T_0301 

EXPETED OUTPUT

USER_ID | COURSE_ID | CLASS_ID 
00001 | C_0001 | T_0001 
00001 | C_0001 | T_0002 

任何人都可以幫我嗎?

謝謝!

+1

將'COUNT(*)OVER(PARTITION BY USER_ID,COURSE_ID)AS cnt'添加到您的現有查詢中,並將其放入派生表(內聯視圖)並應用WHERE cnt> 1。 – dnoeth

+0

@ dnoeth,太棒了!簡單而簡單! –

回答

0

有很多方法可以做到這一點。我通常會使用EXISTS,但由於你有一堆連接,我會堅持。

SELECT eseat.USER_ID, c.COURSE_ID, t.CLASS_ID 
FROM 
    table_CLASS t                  -- Classes 
    LEFT JOIN                   -- This JOIN is necessary to have only the future Classes 
    (
     SELECT aa.CLASS_ID, MIN(aa.START_DATE) AS "START", MAX(aa.END_DATE) AS "END" 
     FROM table_SEG_CLASS aa               -- Segment x Class 
     GROUP BY aa.CLASS_ID 
    ) seg ON seg.CLASS_ID = t.CLASS_ID 
    LEFT JOIN table_ENROLL_CLASS eseat ON eseat.CLASS_ID = t.CLASS_ID     -- Enrollment x Class 
    LEFT JOIN table_ENROLL_STAT estat ON estat.ENRL_STAT_ID = eseat.ENRL_STAT_ID  -- Enrollment status type 
    LEFT JOIN table_COURSE c ON c.COURSE_ID = t.COURSE_ID        -- Course x Class 
-- added this line: user is also enrolled in a different class for this course 
    inner join table_enroll_class eseat2 on eseat2.user_id = eseat.user_id 
     and eseat2.class_id in (select class_id from table_class where course_id = t.course_id and class_id <> eseat.class_id) 
WHERE 
    1=1 
    AND t.CANCELLED = 'N'    -- Only active classes 
    AND estat.ENRL_STAT_TYP_ID <> 'C' -- Not show cancelled enrollment status 
    AND SYSDATE <= "END"    -- To show only future classes 

我想你可能還想讓「eseat」成爲內連接,但這只是一個清晰度問題。

+0

我無法得到應用此預期結果。我應用了'COUNT(*)OVER(PARTITION BY USER_ID,COURSE_ID)',就像@dnoeth提出的那樣。 –

相關問題