我们如何在Oracle中进行像这样的简单联接?

时间:2018-07-02 20:46:09

标签: sql oracle join oracle11g

我们如何进行这样的简单连接?

我有三个表:

表1:学生

UserID
11111111111
22222222222
33333333333
44444444444
55555555555
66666666666
77777777777
88888888888
99999999999

表2:课程

ClassID | StartDate
0001    | Apr 2,2018, 9:00 AM
0002    | May 2,2018, 9:00 AM

表3:注册

UserID      | ClassID
11111111111 | 0001
11111111111 | 0002
22222222222 | 0001
33333333333 | 0002

结果我需要下面的桌子:

期望的表

UserID      | ClassID | StartDate
11111111111 | 0001    | Apr 2,2018, 9:00 AM
11111111111 | 0002    | May 2,2018, 9:00 AM
22222222222 | 0001    | Apr 2,2018, 9:00 AM
33333333333 | 0002    | May 2,2018, 9:00 AM
44444444444 | null    | null
55555555555 | null    | null
66666666666 | null    | null
77777777777 | null    | null
88888888888 | null    | null
99999999999 | null    | null

如果可能,请不要使用UNION

进一步编辑

表3:注册(带有新的“ EnrollStatus”列)

UserID      | ClassID | EnrollStatus
11111111111 | 0001    | Enrolled
11111111111 | 0002    | Enrolled
22222222222 | 0001    | Enrolled
33333333333 | 0002    | Enrolled

期望的表(带有新的“ EnrollStatus”列)

UserID      | ClassID | StartDate           | EnrollStatus
11111111111 | 0001    | Apr 2,2018, 9:00 AM | Enrolled
11111111111 | 0002    | May 2,2018, 9:00 AM | Enrolled
22222222222 | 0001    | Apr 2,2018, 9:00 AM | Enrolled
22222222222 | null    | null                | null
33333333333 | null    | null                | null
33333333333 | 0002    | May 2,2018, 9:00 AM | Enrolled
44444444444 | null    | null                | null
44444444444 | null    | null                | null
55555555555 | null    | null                | null
55555555555 | null    | null                | null
66666666666 | null    | null                | null
66666666666 | null    | null                | null
77777777777 | null    | null                | null
77777777777 | null    | null                | null
88888888888 | null    | null                | null
88888888888 | null    | null                | null
99999999999 | null    | null                | null
99999999999 | null    | null                | null

我认为此JOIN是必要的,因为我还有另一张表格(其中附有年份的日期),并且我需要计算用户在特定时期内未注册的星期数(过滤器)。此外,需要显示用户尚未按用户注册的星期数。

表4:日期(来自:SQL working week in Oracle

WITH DATES AS
(
  SELECT DATE '2017-12-25' + LEVEL -1 dt FROM DUAL CONNECT BY LEVEL <= 500
)
SELECT dt,TO_CHAR(dt,'DY') DAY,TO_CHAR(dt,'WW') WW,TO_CHAR(dt,'IW') IW,
   CASE WHEN TO_CHAR(dt,'D')<TO_CHAR(TO_DATE(TO_CHAR(dt,'YYYY')||'0101','YYYYMMDD'),'D') THEN 
     LPAD(TO_CHAR(dt,'WW')+1,2,'0')
   ELSE 
     TO_CHAR(dt,'WW')
   END MY
FROM dates

查询结果

DT                    | DAY | WW | IW | MY
Dec 25, 2017, 3:00 AM | MON | 52 | 52 | 52
Dec 26, 2017, 3:00 AM | TUE | 52 | 52 | 52
Dec 27, 2017, 3:00 AM | WED | 52 | 52 | 52
Dec 28, 2017, 3:00 AM | THU | 52 | 52 | 52
Dec 29, 2017, 3:00 AM | FRI | 52 | 52 | 52
Jan 1, 2018, 3:00 AM  | MON | 01 | 01 | 01
Jan 2, 2018, 3:00 AM  | TUE | 01 | 01 | 01
Jan 3, 2018, 3:00 AM  | WED | 01 | 01 | 01
Jan 4, 2018, 3:00 AM  | THU | 01 | 01 | 01
Jan 5, 2018, 3:00 AM  | FRI | 01 | 01 | 01
Jan 8, 2018, 3:00 AM  | MON | 02 | 02 | 02
Jan 9, 2018, 3:00 AM  | TUE | 02 | 02 | 02
Jan 10, 2018, 3:00 AM | WED | 02 | 02 | 02
Jan 11, 2018, 3:00 AM | THU | 02 | 02 | 02
Jan 12, 2018, 3:00 AM | FRI | 02 | 02 | 02
Jan 15, 2018, 3:00 AM | MON | 03 | 03 | 03
Jan 16, 2018, 3:00 AM | TUE | 03 | 03 | 03
Jan 17, 2018, 3:00 AM | WED | 03 | 03 | 03
Jan 18, 2018, 3:00 AM | THU | 03 | 03 | 03
Jan 19, 2018, 3:00 AM | FRI | 03 | 03 | 03

1 个答案:

答案 0 :(得分:4)

您可以尝试基于LEFT JOIN表使用Student

SELECT s.*,e.*,c.* 
  FROM Student s 
  LEFT JOIN Enroll e on s.UserID =e.UserID
  LEFT JOIN Class c on c.ClassID =e.ClassID 
 ORDER BY s.UserID

Sqlfiddle

编辑

我看到您在编辑问题,您似乎想要获得笛卡尔积。

您需要先CROSS JOIN在学生桌和班级上,然后LEFT JOIN

SELECT s.userID,
       e.CLASSID,
       (CASE WHEN e.CLASSID IS NULL then NULL else c.STARTDATE END) STARTDATE,
       e.EnrollStatus
FROM 
Student s 
CROSS JOIN Class c 
LEFT JOIN Enroll e
    on s.UserID =e.UserID AND e.CLASSID = c.CLASSID
ORDER BY s.UserID

结果

|      USERID | CLASSID |           STARTDATE | ENROLLSTATUS |
|-------------|---------|---------------------|--------------|
| 11111111111 |    0001 | Apr 2,2018, 9:00 AM | EnrollStatus |
| 11111111111 |    0002 | May 2,2018, 9:00 AM | EnrollStatus |
| 22222222222 |    0001 | Apr 2,2018, 9:00 AM | EnrollStatus |
| 22222222222 |  (null) |              (null) |       (null) |
| 33333333333 |  (null) |              (null) |       (null) |
| 33333333333 |    0002 | May 2,2018, 9:00 AM | EnrollStatus |
| 44444444444 |  (null) |              (null) |       (null) |
| 44444444444 |  (null) |              (null) |       (null) |
| 55555555555 |  (null) |              (null) |       (null) |
| 55555555555 |  (null) |              (null) |       (null) |
| 66666666666 |  (null) |              (null) |       (null) |
| 66666666666 |  (null) |              (null) |       (null) |
| 77777777777 |  (null) |              (null) |       (null) |
| 77777777777 |  (null) |              (null) |       (null) |
| 88888888888 |  (null) |              (null) |       (null) |
| 88888888888 |  (null) |              (null) |       (null) |
| 99999999999 |  (null) |              (null) |       (null) |
| 99999999999 |  (null) |              (null) |       (null) |

CORSS JOIN