Newbie T-SQL ? Using Joins

  • Hello all, this my first time posting,but I have read several of the killer articles from the site. I am a newbie to T-SQL. I am trying to do the following. I have tried left outer join and using a subquery..results very from duplicates to an empty table.

    Table1

    Column1 Column2

    A 1

    B 1

    C 1

    A 2

    B 2

    C 2

    D 2

    Table 2

    Column1

    A

    B

    C

    D

    Output Table should look like as I am only interest in values that are in T2 that are not in T1 any give value of T1C2

    Column1 Column2

    D 1

    Any light will be appreciated greatly

  • You are looking at something like this:

    declare @Table1 table(

    Col1 char(1),

    Col2 int

    );

    declare @Table2 table(

    Col1 char(1)

    );

    insert into @Table1

    values ('A',1),

    ('B',1),

    ('C',1),

    ('A',2),

    ('B',2),

    ('C',2),

    ('D',2);

    insert into @Table2

    values ('A'),('B'),('C'),('D');

    select * from @Table1;

    select * from @Table2;

    with UniqCol2 as (

    select distinct Col2 from @Table1

    ), ExcludedData as (

    select

    t2.Col1,

    uc.Col2

    from

    @Table2 t2

    cross apply UniqCol2 uc

    except

    select

    t1.Col1,

    t1.Col2

    from

    @Table1 t1)

    select Col1 from ExcludedData;

  • Thanks that is excactly what I was looking for. I am not familiar with cross apply,but look forward to using it.

    Cheers!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply