SQL Server查询结果意外

时间:2018-03-08 23:18:55

标签: sql sql-server azure-sql-database

我试图对一个大表进行排序,我们将使用另一个表中的值来调用此Table1,我们将调用此Table2来过滤结果。不幸的是,我的查询结果显示的值与我的过滤器不匹配,在表2中找到了值。

这是我使用的查询:

SELECT Table1.* FROM Table1 
JOIN Table2 on Table1.ColumnX = Table2.ColumnX
AND    Table1.DateAddedToMarket >= '2018-01-01' 
AND    Table1.DateAddedToMarket < '2018-02-01' 
UNION
SELECT Table1.* FROM Table1 
JOIN Table2 on Table1.ColumnX = Table2.ColumnY
AND    Table1.DateAddedToMarket >= '2018-01-01' 
AND    Table1.DateAddedToMarket < '2018-02-01' 
ORDER BY Table1.DateAddedToMarket DESC

这就是Table2的样子:

+--------+--------------+
|ColumnX |    ColumnY   |
+--------+--------------+
| 872510 |       879962 |
| 872514 |       879963 |
| 872511 |              |
| 872515 |              |
| 872512 |              |
| 879529 |              |
| 872513 |              |
| 879530 |              |
| 879959 |              |
| 879960 |              |
| 879961 |              |
+--------+--------------+

这就是Table1的样子:

+---------+-------------------+
|ColumnX  | DateAddedToMarket |
+---------+-------------------+
|         |                   |
| 872510  | 2018-01-01        |
| 872514  | 2018-01-02        |
| 872511  | 2018-01-03        |
| 872515  | 2018-01-04        |
| 872512  | 2018-01-05        |
| 879529  | 2018-03-01        |
| 872513  | 2018-03-02        |
| 879530  | 2018-03-03        |
| 879959  | 2018-03-04        |
| 879960  | 2018-01-05        |
| 879961  | 2018-01-06        |
| 879962  | 2018-01-07        |
| 879963  | 2018-01-08        |
| 379964  | 2018-01-09        |
| 379978  | 2018-01-10        |
| 379979  | 2018-01-11        |
| 379980  | 2018-03-04        |
| 513907  | 2018-03-05        |
| 517156  | 2018-03-06        |
| 519984  | 2018-03-07        |
| 547211  | 2018-03-08        |
| 547270  | 2018-03-09        |
| 552874  | 2018-03-10        |
| 552952  | 2018-03-11        |
|         |                   |
+---------+-------------------+

日期过滤器完美运行;但是,结果中会有一些值在表2中找不到。

预期结果:

+---------+-------------------+
|ColumnX  | DateAddedToMarket |
+---------+-------------------+
|         |                   |
| 872510  | 2018-01-01        |
| 872514  | 2018-01-02        |
| 872511  | 2018-01-03        |
| 872515  | 2018-01-04        |
| 872512  | 2018-01-05        |
| 879960  | 2018-01-05        |
| 879961  | 2018-01-06        |
| 879962  | 2018-01-07        |
| 879963  | 2018-01-08        |
+---------+-------------------+

3 个答案:

答案 0 :(得分:0)

将过滤器从on子句中取出并将它们放在where子句中。

SELECT Table1.* FROM Table1 
JOIN Table2 on Table1.ColumnX = Table2.ColumnX
WHERE Table1.DateAddedToMarket >= '2018-01-01' 
AND    Table1.DateAddedToMarket < '2018-02-01' 
UNION
SELECT Table1.* FROM Table1 
JOIN Table2 on Table1.ColumnX = Table2.ColumnY
WHERE Table1.DateAddedToMarket >= '2018-01-01' 
AND    Table1.DateAddedToMarket < '2018-02-01' 
ORDER BY Table1.DateAddedToMarket DESC

答案 1 :(得分:0)

没有工会可能会更好。

SELECT Table1.* FROM Table1 
WHERE Table1.DateAddedToMarket >= '2018-01-01' 
AND    Table1.DateAddedToMarket < '2018-02-01' 
AND  (Table1.ColumnX in (select columnX from table2)
     or Table1.columnX (in select columnY from table2))
ORDER BY Table1.DateAddedToMarket DESC

答案 2 :(得分:0)

我不确定你为什么要使用UNION,而你只需要一次连接即可。像这样:

SELECT 
    t1.ColumnX, 
    t1.DateAddedToMarket
FROM Table1 t1
INNER JOIN Table2 t2 ON t2.ColumnX = t1.ColumnX OR t2.ColumnY = t1.ColumnX
WHERE 
    t1.DateAddedToMarket BETWEEN '2018-01-01' AND '2018-02-01'

输出

| ColumnX | DateAddedToMarket |
|---------|-------------------|
|  872510 |        2018-01-01 |
|  872514 |        2018-01-02 |
|  872511 |        2018-01-03 |
|  872515 |        2018-01-04 |
|  872512 |        2018-01-05 |
|  879960 |        2018-01-05 |
|  879961 |        2018-01-06 |
|  879962 |        2018-01-07 |
|  879963 |        2018-01-08 |

小提琴:Live Preview