SQL:LEFT OUTER JOIN不返回任何行

时间:2016-06-14 16:18:04

标签: sql sql-server join left-join

我有一个使用LEFT OUTER JOIN合并2个数据集的查询。我知道两个数据集都应该返回数据,因为我分别运行了超级查询和子查询。出于某种原因,查询返回零结果。谁知道为什么?

左数据:

item        FG_lots
447845      E2211

正确的数据:

candy_lot_check    candy_item
L2211              835116

预期结果:

item        FG_lots   candy_lot_check    candy_item
447845      E2211     null               null

我的查询失败的结果(无结果):

item        FG_lots   candy_lot_check    candy_item

查询:

--Initialization--
DECLARE @Item NVARCHAR(30) = '447845'
DECLARE @Date datetime = '6/13/2016'
SET DATEFIRST 1;

DECLARE @client NVARCHAR(20)
SET @client = (SELECT i.Uf_ClientName FROM item AS i WHERE i.item = @Item)

DECLARE @count integer

--Query--
SET @count = (CASE 

                WHEN (@client = 'A' OR @client = 'B')
                    THEN 4 
                WHEN @client = 'C'
                    THEN 3
                WHEN @client = 'D' 
                    THEN 5
                ELSE
                    4
              END)


SELECT DISTINCT

    t.item,
    LEFT(t.lot,@count) AS FG_lots,
    (CASE
        WHEN candylot.candy_lots IS NULL
        THEN 'NO MATCH'
        ELSE candylot.candy_lots
    END) AS candy_lot_check,
    (CASE
        WHEN candylot.item IS NULL
        THEN 'NO MATCH'
        ELSE candylot.item
    END) AS candy_item


FROM

    ISW_LPTrans AS t
    LEFT OUTER JOIN

        (
            SELECT

                t.item,
                LEFT(t.lot,@count) AS candy_lots,
                t.ref_num AS job,
                t.ref_line_suf AS suffix

            FROM

                ISW_LPTrans AS t
                INNER JOIN item AS i on i.item = t.item

            WHERE

                i.product_code = 'RM-Candy' AND
                t.trans_date = @Date AND
                t.trans_type = 'I' AND
                t.ref_num IN

                    (
                        SELECT TOP 1

                            j.job

                        FROM

                            job AS j

                        WHERE

                            j.item = @Item AND
                            j.job_date = (SELECT DATEADD(DAY, 1-DATEPART(WEEKDAY, @Date), @Date))

                        ORDER BY

                            j.job

                    ) 

                AND t.ref_line_suf IN

                    (

                        SELECT TOP 1

                            j.suffix

                        FROM

                            job AS j

                        WHERE

                            j.item = @Item AND
                            j.job_date = (SELECT DATEADD(DAY, 1-DATEPART(WEEKDAY, @Date), @Date))       

                    ) 

            GROUP BY

                t.item,
                t.lot,
                t.ref_num,
                t.ref_line_suf

            ) AS candylot ON LEFT(t.lot, @count) = candylot.candy_lots 

WHERE

    t.ref_num = candylot.job AND
    t.ref_line_suf = candylot.suffix AND                    
    t.trans_type = 'F' AND
    t.item = @Item AND
    t.trans_date = @Date

1 个答案:

答案 0 :(得分:0)

你有两个'''别名,试着改变它。

似乎你可以重写它以使其更具可读性(加入,而不是缩进),它可能是逻辑中的某些东西不正确