【问题标题】:Extract rows where ID is matching but suffix is not present in another tableExtract rows where ID is matching but suffix is not present in another table
【发布时间】:2022-12-02 01:29:18
【问题描述】:

I have a scenario where two tables (table1 has columns Id and IDSuffix and table2 has columns TableID, TableIDSuffix and Name) have matching ID, but the suffix associated with ID may or may not present in Table2.

How to identify those suffixes which is present in table1, but absent in table2?

Table1

ID IDSuffix
101 0
101 8
101 9
412 0
412 5
412 9
215 0
215 9
518 0
518 9

Table2

TableID TableIDSuffix Name
101 0 Tom
101 0 Mel
101 9 Tom
101 9 Mel
412 0 Gab
412 9 Gab
215 0 Kit
215 0 Hary
215 9 Hary
518 0 Jo
518 9 J0
518 0 Kia
518 9 Kia

Required output should be like this:

ID IDSuffix Name
101 8 Tom
101 8 Mel
412 5 Gab
215 9 Kit

I used the left join in my query like this:

SELECT a.ID, a.IDSuffix, b.TableIDSuffix, b.Name
FROM Table1 a
LEFT JOIN Table2 b ON a.ID = b.TableID 
                   AND a.IDSuffix = b.TableIDSuffix
GROUP BY a.ID, a.IDSuffix, b.TableIDSuffix, b.Name

I was expecting b.TableIDSuffix will be NULL so that I can extract those rows, but what I see for example for ID = 101 for IDSuffix 8 there is only one NULL in TableIDSuffix - there should be two for both Tom and Mel as shown in the expected result table.

Thanks for help

【问题讨论】:

    标签: sql


    【解决方案1】:

    may you please check your required output? The final record for 'kit' should not appear, because there is no '215','9' for 'kit' in table 2.

    Below is code to run your example data through any local session in SSMS:

    IF OBJECT_ID('TEMPDB..#table1') IS NOT NULL
        DROP TABLE #table1
    CREATE TABLE #table1(
        ID          INT
        ,IDSuffix   INT
    )
    INSERT INTO #table1 (ID,IDSuffix)
    VALUES (101,0)
    ,(101,8)
    ,(101,9)
    ,(412,0)
    ,(412,5)
    ,(412,9)
    ,(215,0)
    ,(215,9)
    ,(518,0)
    ,(518,9)
    
    IF OBJECT_ID('TEMPDB..#table2') IS NOT NULL
        DROP TABLE #table2
    CREATE TABLE #table2(
        TableID         INT
        ,TableIDSuffix  INT
        ,[Name]         VARCHAR(5)
    )
    INSERT INTO #table2 (TableID,TableIDSuffix,[Name])
    VALUES (101,0,'Tom')
    ,(101,0,'Mel')
    ,(101,9,'Tom')
    ,(101,9,'Mel')
    ,(412,0,'Gab')
    ,(412,9,'Gab')
    ,(215,0,'Kit')
    ,(215,0,'Hary')
    ,(215,9,'Hary')
    ,(518,0,'Jo')
    ,(518,9,'J0')
    ,(518,0,'Kia')
    ,(518,9,'Kia')
    

    Here is my solution to your problem:

    SELECT A.ID
        ,A.IDSuffix
        ,B.[Name]
        ,C.TableID
    FROM #table1 A
        INNER JOIN (
            SELECT DISTINCT
                b.TableID
                ,b.[Name]
            FROM #table2 b
            ) B
            ON A.ID = B.TableID
         LEFT OUTER JOIN #table2 C
            ON A.ID = C.TableID
            AND A.IDSuffix = C.TableIDSuffix
            AND b.[Name] = C.[Name]
    WHERE C.TableID IS NULL
    

    In this case, your second table needed to be restructured for all existing combinations of name and ID. We can join that to T1 for all possible combinations of all 3 columns.

    Then, we can filter for the mis-matches between this adjusted T1 and T2.

    Then with the WHERE clause:

    Hope this helps!

    【讨论】:

    • Thanks alot. But if you see ID 215 (Table1) , Hary (Table2) is on both suffixes (0 and 9) But Kit is only on 0 . So the resut set has missing record of Kit with 9 Suffix.
    • Thanks alot. But if you see ID 215 (Table1) , Hary (Table2) is on both suffixes (0 and 9) But Kit is only on 0 . So the query resut has missing record of Kit with 9 Suffix.
    • A little harder than I thought. Adjusted the answer above!
    猜你喜欢
    • 2018-11-14
    • 1970-01-01
    • 2018-12-08
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-17
    相关资源
    最近更新 更多