SQL Left join在连接列

时间:2018-05-24 15:04:38

标签: sql sql-server join

我甚至不确定这是否可行,但我试图加入两张桌子,但我没有得到预期的结果。我的查询如下:

SELECT inc.NUMBER as TICKET,
inc.UNIV_NUM,
inc.ASSIGNEE,
work.SUBMIT_DATE
work.TYPE
FROM dbo.HELP_DESK as inc
LEFT JOIN dbo.WORKLOG as work on inc.NUMBER = work.NUMBER
Where inc.ASSIGNEE = 'AB049732'
and work.SUBMIT_DATE = (Select MAX(work2.SUBMIT_DATE) 
                        from dbo.WORKLOG as work2 
                        where work2.NUMBER = work.NUMBER
                        and work2.TYPE = '16000')

我的表格看起来像这样

INC

+---------+-----------+----------+
| NUMBER  | UNIV_NUM  | ASSIGNEE |
+---------+-----------+----------+
|  100001 |   4321781 | AB049732 |
|  100002 |   4232756 | AB049732 |
|  100003 |   4322534 | AB049732 |
|  100004 |   4328534 | AB049732 |
+---------+-----------+----------+

工作

+--------+------------+-------+
| NUMBER |   DATE     | TYPE  |
+--------+------------+-------+
| 100001 | 23/05/2018 | 16000 |
| 100003 | 22/05/2018 | 16000 |
| 100004 | 22/05/2018 | 16000 |
+--------+------------+-------+

我的预期输出是:

+--------+----------+----------+------------+-------+
| NUMBER | UNIV_NUM | ASSIGNEE |    DATE    | TYPE  |
+--------+----------+----------+------------+-------+
| 100001 |  4321781 | AB049732 | 23/05/2018 | 16000 |
| 100002 |  4232756 | AB049732 | NULL       | NULL  |
| 100003 |  4322534 | AB049732 | 22/05/2018 | 16000 |
| 100004 |  4328534 | AB049732 | 22/05/2018 | 16000 |
+--------+----------+----------+------------+-------+

但我的实际输出是:

+---------+-----------+----------+------------+-------+
| NUMBER  | UNIV_NUM  | ASSIGNEE |   DATE     | TYPE  |
+---------+-----------+----------+------------+-------+
|  100001 |   4321781 | AB049732 | 23/05/2018 | 16000 |
|  100003 |   4322534 | AB049732 | 22/05/2018 | 16000 |
|  100004 |   4328534 | AB049732 | 22/05/2018 | 16000 |
+---------+-----------+----------+------------+-------+

实际上,尽管在inc表中,数字100002仍然不显示。我做错了什么,或者这是一个你不能加入到那些不存在的东西的案例?

3 个答案:

答案 0 :(得分:3)

您的加入条件不好。试试这个:

SELECT inc.NUMBER as TICKET,
inc.UNIV_NUM,
inc.ASSIGNEE,
work.SUBMIT_DATE
work.TYPE
FROM dbo.HELP_DESK as inc
LEFT JOIN dbo.WORKLOG as work on inc.NUMBER = work.NUMBER
and work.SUBMIT_DATE = (Select MAX(work2.SUBMIT_DATE) 
                        from dbo.WORKLOG as work2 
                        where work2.NUMBER = work.NUMBER
                        and work2.TYPE = '16000')
Where inc.ASSIGNEE = 'AB049732'

看到区别?如果将work.SUBMIT_DATE = ...条件放在Where子句中(就像您所做的那样),那么您的连接将成为内连接。但是你想要一个外部联接。

答案 1 :(得分:1)

使用窗口功能!

app:cardElevation="0dp"

这与您的查询略有不同,但我认为这是您真正想要的逻辑。您的查询将查找具有“16000”类型的最大提交日期的记录,无论类型如何。我认为您确实希望类型与提交日期保持一致。

如果这种解释错误,可以很容易地调整查询:

SELECT h.NUMBER as TICKET, h.UNIV_NUM, h.ASSIGNEE,
       w.SUBMIT_DATE, w.TYPE
FROM dbo.HELP_DESK h LEFT JOIN
     (SELECT w.*, MAX(w2.SUBMIT_DATE) OVER (PARTITION BY w.NUMBER) as max_submit_date
      FROM dbo.WORKLOG w
      WHERE w.TYPE = '16000'
     ) w
     ON h.NUMBER = w.NUMBER AND w.submit_date = w.max_submit_date
WHERE h.ASSIGNEE = 'AB049732';

这些版本不仅更简单,而且还应具有更好的性能。

答案 2 :(得分:1)

你可以使用这样的CTE:

WITH WorkDates
    AS (SELECT SUBMIT_DATE,TYPE
        From WORKLOG work
        Where SUBMIT_DATE = (Select MAX(work2.SUBMIT_DATE) 
                        from dbo.WORKLOG as work2 
                        where work2.NUMBER = work.NUMBER
                        and work2.TYPE = '16000'))
SELECT inc.NUMBER as TICKET,
inc.UNIV_NUM,
inc.ASSIGNEE,
WorkDates.SUBMIT_DATE
WorkDates.TYPE
FROM dbo.HELP_DESK as inc
LEFT JOIN WorkDates on inc.NUMBER = WorkDates.NUMBER
Where inc.ASSIGNEE = 'AB049732'