SQL:LEFT OUTER JOIN重复某些行

时间:2016-06-13 17:30:48

标签: sql sql-server tsql left-join

我有2个名单:

1)

FG       FG lot
12043267 6105552268 
12043267 6109552268  
12043267 6110552268  
12043267 6111552268 

2)

item     item lot
43626551 610457472H 
43626551 610557472H 
43626551 610957471H 
43626551 610957472H 
43626551 611057472H 
43626551 611157471H 
43626551 611157472H 

应该发生什么:

FG       FG lot      item lot    item
12043267 6105552268  610557472H  43626551 
12043267 6109552268  610957471H  43626551 
12043267 6110552268  611057472H  43626551 
12043267 6111552268  611157471H  43626551 

总体思路:检查所有FG批次是否都有相应的项目批次。

棘手的部分是我需要查看前4位数字进行比较,然后才考虑是否有一个相应的项目批次。也就是说,我不在乎右边是否有3个,4个,5个,99个项目批次,只要至少有1个相应的项目批次。

我的代码:

DECLARE @FG NVARCHAR(30) = '12043267'
DECLARE @proddate datetime = '6/8/2016'
SET DATEFIRST 1;

SELECT DISTINCT

    t.item,
    t.lot,
    candylots.lot,
    candylots.item

FROM

    ISW_LPTrans AS t
    LEFT OUTER JOIN

        (SELECT TOP 1

        --t.lp_num,
        t.item,
        t.lot

        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 = '6/8/2016' AND
            t.trans_type = 'I' AND
            t.ref_num IN

                (

                    SELECT

                        j.job

                    FROM

                        job AS j

                    WHERE

                        j.item = '12043267' AND
                        j.job_date = '6/6/2016'

                )

            AND t.ref_line_suf IN

                (

                    SELECT

                        j.suffix

                    FROM

                        job AS j

                    WHERE

                        j.item = '12043267' AND
                        j.job_date = '6/6/2016'     

                ) 

        GROUP BY

            t.item,
            t.lot

        ) AS candylots ON LEFT(candylots.lot, 4) = LEFT(t.lot, 4)

WHERE

    t.ref_num = 'N000016174' AND
    t.ref_line_suf = 24 AND
    t.trans_type = 'F' AND
    t.item = '12043267' AND
    t.trans_date = '6/8/2016'

/*GROUP BY

    t.item,
    t.lot,
    candylots.lot,
    candylots.item*/

代码的作用:

FG       FG lot      item lot  item
12043267 6105552268  null      null 
12043267 6109552268  null      null 
12043267 6110552268  null      null 
12043267 6111552268  null      null 
显然有些不对劲。有没有人知道实现这个目标的方法?

更新

我只尝试SELECTING两个列表的前四位数字并且它可以正常工作,但理想情况下我希望能够完整地写出这两个列表。< / p>

DECLARE @FG NVARCHAR(30) = '12043267'
DECLARE @proddate datetime = '6/8/2016'
SET DATEFIRST 1;

SELECT DISTINCT

    t.item,
    LEFT(t.lot,4) AS FG_lots,
    candylot.candy_lots,
    candylot.item

FROM

    ISW_LPTrans AS t
    LEFT OUTER JOIN

        (SELECT

            --t.lp_num,
            t.item,
            LEFT(t.lot,4) AS candy_lots

            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 = '6/8/2016' AND --(SELECT DATEADD(DAY, 1-DATEPART(WEEKDAY, @proddate), @proddate)) AND
                t.trans_type = 'I' AND
                t.ref_num IN

                    (

                        SELECT

                            j.job

                        FROM

                            job AS j

                        WHERE

                            j.item = '12043267' AND
                            j.job_date = '6/6/2016'

                    )

                AND t.ref_line_suf IN

                    (

                        SELECT

                            j.suffix

                        FROM

                            job AS j

                        WHERE

                            j.item = '12043267' AND
                            j.job_date = '6/6/2016'     

                    ) 

            GROUP BY

                t.item,
                t.lot

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

WHERE

    t.ref_num = 'N000016174' AND
    t.ref_line_suf = 24 AND
    t.trans_type = 'F' AND
    t.item = '12043267' AND
    t.trans_date = '6/8/2016'

正确但不理想的结果:

item     FG_lots  candy_lots  item1
12043267 6105     6105        43626551 
12043267 6109     6109        43626551 
12043267 6110     6110        43626551 
12043267 6111     6111        43626551

1 个答案:

答案 0 :(得分:0)

它需要是这样的吗?

SELECT t.item, t.lot, i.FG, i.[FG lot]
FROM ISW_LPTrans as t
LEFT JOIN item as i
ON LEFT(i.[FG lot],4) = LEFT(t.lot,4);

还是太简单了?