LEFT JOIN单行并使用单行的值填充NULL值

时间:2016-02-07 15:14:26

标签: sql-server

我有:

使用服装类别中的项目的第一个订单:

    SELECT DISTINCT TOP 1
        od.SalesOrderNumber,
        a.OrderDate,
        od.hasClothing
    FROM History a
    INNER JOIN OrderDetails od
        ON a.SalesOrderNumber=od.SalesOrderNumber
    WHERE od.hasClothing = 1
    ORDER BY a.OrderDate ASC 

+------------------+------------+-------------+
| SalesOrderNumber | KeyDate    | hasClothing |
+------------------+------------+-------------+
| SO57673          | 2007-11-09 |           1 |
+------------------+------------+-------------+

和......

产生的另一个查询:

+------------------+----------------+------------+
| SalesOrderNumber | CustomerAltKey | OrderDate  |
+------------------+----------------+------------+
| SO44063          | AW00011240     | 2005-08-30 |
| SO51205          | AW00011240     | 2007-07-03 |
| SO57673          | AW00011240     | 2007-11-09 |
| SO57953          | AW00011240     | 2007-11-14 |
+------------------+----------------+------------+

我试图加入两个表并产生:

+------------------+----------------+------------+----------------------+
| SalesOrderNumber | CustomerAltKey | OrderDate  | OrderDate >= KeyDate |
+------------------+----------------+------------+----------------------+
| SO44063          | AW00011240     | 2005-08-30 |                    0 |
| SO51205          | AW00011240     | 2007-07-03 |                    0 |
| SO57673          | AW00011240     | 2007-11-09 |                    1 |
| SO57953          | AW00011240     | 2007-11-14 |                    1 |
+------------------+----------------+------------+----------------------+

我在SalesOrderNumber,OrderDate等上尝试了LEFT OUTER JOIN,并尝试将空值加入2007-11-09日期......但我尝试的所有内容都失败了。我得到:

+------------------+----------------+------------+----------------------+
| SalesOrderNumber | CustomerAltKey | OrderDate  | OrderDate >= KeyDate |
+------------------+----------------+------------+----------------------+
| SO44063          | AW00011240     | 2005-08-30 | NULL                 |
| SO51205          | AW00011240     | 2007-07-03 | NULL                 |
| SO57673          | AW00011240     | 2007-11-09 | 1                    |
| SO57953          | AW00011240     | 2007-11-14 | NULL                 |
+------------------+----------------+------------+----------------------+

有什么建议吗?这让我疯了。

2 个答案:

答案 0 :(得分:0)

由于您需要在查询结果中获取该字段,因此需要使用CASE语句。

如果您发布表格的定义,我们可以帮助您。同时解决方案应该是这样的:

SELECT DISTINCT TOP 1
    od.SalesOrderNumber,
    a.OrderDate,
    od.hasClothing,
    CASE WHEN a.OrderDate >= od.KeyDate THEN 1 ELSE 0 END AS RESULT_FIELD_NAME
FROM History a
INNER JOIN OrderDetails od
    ON a.SalesOrderNumber=od.SalesOrderNumber
WHERE od.hasClothing = 1
ORDER BY a.OrderDate ASC 

希望它有所帮助。

答案 1 :(得分:0)

根据您的示例数据我创建了这个。但是,根据示例数据,您似乎并不关心加入SalesOrderNumber。因此,我CROSS JOIN编辑了集合以获得预期的输出。

SELECT 
    r.SalesOrderNumber,r.CustomerAltKey,r.OrderDate,
    [OrderDate>=KeyDate]=CAST(CASE WHEN l.KeyDate<=r.OrderDate THEN 1 ELSE 0 END AS BIT)
FROM
    (
        SELECT SalesOrderNumber='SO57673',KeyDate='20071109',hasClothing=1
    ) AS l
    CROSS JOIN (
        SELECT SalesOrderNumber='SO44063',CustomerAltKey='AW00011240',OrderDate='20050830' UNION ALL
        SELECT SalesOrderNumber='SO51205',CustomerAltKey='AW00011240',OrderDate='20070703' UNION ALL
        SELECT SalesOrderNumber='SO57673',CustomerAltKey='AW00011240',OrderDate='20071109' UNION ALL
        SELECT SalesOrderNumber='SO57953',CustomerAltKey='AW00011240',OrderDate='20071114' 
    ) AS r

结果:

+------------------+----------------+-----------+--------------------+
| SalesOrderNumber | CustomerAltKey | OrderDate | OrderDate>=KeyDate |
+------------------+----------------+-----------+--------------------+
| SO44063          | AW00011240     |  20050830 |                  0 |
| SO51205          | AW00011240     |  20070703 |                  0 |
| SO57673          | AW00011240     |  20071109 |                  1 |
| SO57953          | AW00011240     |  20071114 |                  1 |
+------------------+----------------+-----------+--------------------+