Tips for matching data on multiple columns in two tables

  • I'm a new to T-SQL.  My background is IT support & sys admin work; not the art coding unfortunately...  I've installed SQL Server instances more times than I can remember anymore and now I'm venturing into the T-SQL side.

    I have two tables I am working with called BM3 (883 total rows) & BM3_HT_WT (747445 total rows); each table has the exact same columns (30+).  There is a column in each table called [Service_Type_CD].  The contents of [Service_Type_CD] is all 'BM3' in table BM3.  The contents of [Service_Type_CD] in BM3_HT_WT is either 'WT' or 'HT'

    The rest of the columns & contents contain demographics (which have been changed for the purpose of this post).

    The end goal is to determine if any of the people in table BM3 that have 'BM3' in [Service_Type_CD] are also in table BM3_HT_WT with 'HT' and 'WT' in the [Service_Type_CD] column that were all on the same date.  My expectation is to have three rows per person in the output from the query.

    Here's what I've done so far:


    select * from BM3_HT_WT where MBR_LAST_NAME = 'SMITH' and MBR_FIRST_NAME = 'JOHN'
    select * from BM3 where MBR_LAST_NAME = 'SMITH' and MBR_FIRST_NAME = 'JOHN'
    --This shows me through visual inspection that John Smith did have a
    BM3, HT, and WT, all on 01-JUL-2017.
    --He also has other records in BM3_HT_WT that I don't need because their dates don't match up.

    Using that simple query I did see that Smith, John had a record corresponding to the date 01-JUL-2017 in both tables.  

    When I try to write something to do a big version of what I did above with a join, I'm not getting it correct because I should at least get a result with for the three total rows with the person above.  Being new and trying to understand joins from what I've read I think I would use an inner join, but the results are not what I'm expecting. I am getting the two corresponding HT and WT rows for each person, like Smith, John, that have the same [SERVICE_DT], which is GOOD, but record I know is in BM3 isn't being returned from BM3 with the same date.  I feel like I'm close.


    select * from BM3_HT_WT

    inner join BM3
    on BM3_HT_WT.MBR_LAST_NAME = bm3.MBR_LAST_NAME
    and bm3.SERVICE_DT = BM3_HT_WT.SERVICE_DT
    and bm3.MBR_FIRST_NAME = BM3_HT_WT.MBR_FIRST_NAME
    and bm3.SERVICE_TYPE_CD = 'BM3'

    order by bm3.MBR_LAST_NAME, bm3.SERVICE_DT desc

    --Results are 1633 Rows - no BM3's in service_type_cd

    My goal is to get an output that is similar to this (I've just removed the 24+ columns)

    JOHN SMITH M 1-Jul-17 WT
    JOHN SMITH M 1-Jul-17 HT
    JOHN SMITH M 1-Jul-17 BM3

    Here's code to build out a small test table if anyone is willing to assist a newb, thanks in advance!


    Create Database TestData00

    GO

    USE TestData00

    GO

    -------------------------
    -- Create BM3 table
    -------------------------
    CREATE TABLE BM3
    (
    MBR_LAST_NAME char(50) NULL ,
    MBR_FIRST_NAME  char(50) NULL ,
    MBR_SEX char(1) NULL ,
    SERVICE_DT char(11) NULL ,
    SERVICE_TYPE_CD char(3) NULL ,
    );

    -------------------------
    -- Create BM3_HT_WT table
    -------------------------
    CREATE TABLE BM3_HT_WT
    (
    MBR_LAST_NAME char(50) NULL ,
    MBR_FIRST_NAME  char(50) NULL ,
    MBR_SEX char(1) NULL ,
    SERVICE_DT char(11) NULL ,
    SERVICE_TYPE_CD char(3) NULL ,
    );

    ---------------------------
    -- Populate BM3 table
    ---------------------------
    INSERT INTO BM3(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('SMITH', 'JOHN', 'M', '01-JUL-2017', 'BM3');
    INSERT INTO BM3(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('WAYNE', 'BRUCE', 'M', '03-JUL-2017', 'BM3');
    INSERT INTO BM3(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('LINCOLN', 'ABE', 'M', '05-JUL-2017', 'BM3');
    INSERT INTO BM3(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('BROWN', 'EMMET', 'M', '07-JUL-2017', 'BM3');
    INSERT INTO BM3(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('DOE', 'JANE', 'F', '10-JUL-2017', 'BM3');
    INSERT INTO BM3(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('LANE', 'LILY', 'F', '12-JUL-2017', 'BM3');

    -------------------------
    -- Populate BM3_HT_WT table
    -------------------------
    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('SMITH', 'JOHN', 'M', '01-JUL-2017', 'HT');
    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('SMITH', 'JOHN', 'M', '01-JUL-2017', 'WT');
    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('SMITH', 'JOHN', 'M', '01-JUN-2017', 'HT');
    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('SMITH', 'JOHN', 'M', '01-JUN-2017', 'WT');

    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('WAYNE', 'BRUCE', 'M', '03-JUL-2017', 'HT');
    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('WAYNE', 'BRUCE', 'M', '03-JUL-2017', 'WT');
    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('WAYNE', 'BRUCE', 'M', '03-JUN-2017', 'HT');
    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('WAYNE', 'BRUCE', 'M', '03-JUN-2017', 'HT');

    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('LINCOLN', 'ABE', 'M', '05-JUL-2017', 'HT');
    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('LINCOLN', 'ABE', 'M', '05-JUL-2017', 'WT');
    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('LINCOLN', 'ABE', 'M', '05-JUN-2017', 'HT');
    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('LINCOLN', 'ABE', 'M', '05-JUN-2017', 'HT');

    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('BROWN', 'EMMET', 'M', '07-JUL-2017', 'HT');
    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('BROWN', 'EMMET', 'M', '30-JUL-2017', 'WT');
    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('BROWN', 'EMMET', 'M', '29-JUL-2017', 'WT');
    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('BROWN', 'EMMET', 'M', '27-JUL-2017', 'HT');

    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('DOE', 'JANE', 'F', '10-JUL-2017', 'HT');
    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('DOE', 'JANE', 'F', '10-JUL-2017', 'WT');
    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('DOE', 'JANE', 'F', '13-JUN-2017', 'HT');
    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('DOE', 'JANE', 'F', '14-JUN-2017', 'WT');

    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('LANE', 'LILY', 'F', '12-JUL-2017', 'HT');
    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('LANE', 'LILY', 'F', '12-JUL-2017', 'WT');
    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('LANE', 'LILY', 'F', '12-SEP-2017', 'HT');
    INSERT INTO BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('LANE', 'LILY', 'F', '12-SEP-2017', 'WT');

    -Mark
    MSSQL 2019 Standard, Azure Hosted. Techie/Sysadmin by trade; Three years as a "DBA" now.

  • Hope it helps!


    ;WITH dataCTE AS
    (
      SELECT *
      FROM BM3 bm3
      UNION ALL
      SELECT *
      FROM BM3_HT_WT
    )
    , validGroupCTE AS
    (
      SELECT  MBR_FIRST_NAME  = d.MBR_FIRST_NAME
        , MBR_LAST_NAME  = d.MBR_LAST_NAME    
        , SERVICE_DT   = d.SERVICE_DT
      FROM dataCTE d
      GROUP BY d.MBR_LAST_NAME    
        , d.MBR_FIRST_NAME
        , d.SERVICE_DT
      HAVING COUNT(*) = 3        -- Should be Exact 3 Rows in the set, else do not select
       AND COUNT(DISTINCT SERVICE_TYPE_CD) = 3 -- There should be 1 of Each BM3, HT, WT
    )
    SELECT data.*
    FROM dataCTE data
    INNER JOIN validGroupCTE valid
      ON data.MBR_FIRST_NAME = valid.MBR_FIRST_NAME
      AND data.MBR_LAST_NAME = valid.MBR_LAST_NAME
      AND data.SERVICE_DT = valid.SERVICE_DT ;
    GO

  • Try:

    -------------------------
    -- Create BM3 table
    -------------------------
    CREATE TABLE #BM3
    (
      MBR_LAST_NAME char(50)  NULL ,
      MBR_FIRST_NAME  char(50)  NULL ,
      MBR_SEX char(1)  NULL , 
      SERVICE_DT char(11) NULL ,
      SERVICE_TYPE_CD char(3)  NULL ,
    );
    -------------------------
    -- Create BM3_HT_WT table
    -------------------------
    CREATE TABLE #BM3_HT_WT
    (
      MBR_LAST_NAME char(50)  NULL ,
      MBR_FIRST_NAME  char(50)  NULL ,
      MBR_SEX char(1)  NULL , 
      SERVICE_DT char(11) NULL ,
      SERVICE_TYPE_CD char(3)  NULL ,
    );
    ---------------------------
    -- Populate BM3 table
    ---------------------------
    INSERT INTO #BM3(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('SMITH', 'JOHN', 'M', '01-JUL-2017', 'BM3')
    ,('WAYNE', 'BRUCE', 'M', '03-JUL-2017', 'BM3')
    ,('LINCOLN', 'ABE', 'M', '05-JUL-2017', 'BM3')
    ,('BROWN', 'EMMET', 'M', '07-JUL-2017', 'BM3')
    ,('DOE', 'JANE', 'F', '10-JUL-2017', 'BM3')
    ,('LANE', 'LILY', 'F', '12-JUL-2017', 'BM3');
    -------------------------
    -- Populate BM3_HT_WT table
    -------------------------
    INSERT INTO #BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('SMITH', 'JOHN', 'M', '01-JUL-2017', 'HT')
    ,('SMITH', 'JOHN', 'M', '01-JUL-2017', 'WT')
    ,('SMITH', 'JOHN', 'M', '01-JUN-2017', 'HT')
    ,('SMITH', 'JOHN', 'M', '01-JUN-2017', 'WT')
    ,('WAYNE', 'BRUCE', 'M', '03-JUL-2017', 'HT')
    ,('WAYNE', 'BRUCE', 'M', '03-JUL-2017', 'WT')
    ,('WAYNE', 'BRUCE', 'M', '03-JUN-2017', 'HT')
    ,('WAYNE', 'BRUCE', 'M', '03-JUN-2017', 'HT')
    ,('LINCOLN', 'ABE', 'M', '05-JUL-2017', 'HT')
    ,('LINCOLN', 'ABE', 'M', '05-JUL-2017', 'WT')
    ,('LINCOLN', 'ABE', 'M', '05-JUN-2017', 'HT')
    ,('LINCOLN', 'ABE', 'M', '05-JUN-2017', 'HT')
    ,('BROWN', 'EMMET', 'M', '07-JUL-2017', 'HT')
    ,('BROWN', 'EMMET', 'M', '30-JUL-2017', 'WT')
    ,('BROWN', 'EMMET', 'M', '29-JUL-2017', 'WT')
    ,('BROWN', 'EMMET', 'M', '27-JUL-2017', 'HT')
    ,('DOE', 'JANE', 'F', '10-JUL-2017', 'HT')
    ,('DOE', 'JANE', 'F', '10-JUL-2017', 'WT')
    ,('DOE', 'JANE', 'F', '13-JUN-2017', 'HT')
    ,('DOE', 'JANE', 'F', '14-JUN-2017', 'WT')
    ,('LANE', 'LILY', 'F', '12-JUL-2017', 'HT')
    ,('LANE', 'LILY', 'F', '12-JUL-2017', 'WT')
    ,('LANE', 'LILY', 'F', '12-SEP-2017', 'HT')
    ,('LANE', 'LILY', 'F', '12-SEP-2017', 'WT');

    SELECT b.MBR_LAST_NAME
         , b.MBR_FIRST_NAME
         , b.MBR_SEX
         , b.SERVICE_DT
         , b.SERVICE_TYPE_CD
    FROM #BM3 b
    WHERE b.MBR_LAST_NAME IN (SELECT MBR_LAST_NAME FROM #BM3_HT_WT)
    AND b.MBR_FIRST_NAME IN (SELECT MBR_FIRST_NAME FROM #BM3_HT_WT)
    AND b.SERVICE_DT IN (SELECT SERVICE_DT FROM #BM3_HT_WT)
    UNION ALL
    SELECT bhw.MBR_LAST_NAME
         , bhw.MBR_FIRST_NAME
         , bhw.MBR_SEX
         , bhw.SERVICE_DT
         , bhw.SERVICE_TYPE_CD
    FROM #BM3_HT_WT bhw
    WHERE bhw.MBR_FIRST_NAME IN (SELECT MBR_FIRST_NAME FROM #BM3)
    AND bhw.MBR_LAST_NAME IN (SELECT MBR_LAST_NAME FROM #BM3)
    AND bhw.SERVICE_DT IN (SELECT SERVICE_DT FROM #BM3)
    ORDER BY MBR_FIRST_NAME, MBR_LAST_NAME, SERVICE_DT, SERVICE_TYPE_CD;
     DROP TABLE #BM3;
     DROP TABLE #BM3_HT_WT;
    GO

  • Joe Torre - Wednesday, August 16, 2017 4:58 PM

    Try:

    -------------------------
    -- Create BM3 table
    -------------------------
    CREATE TABLE #BM3
    (
      MBR_LAST_NAME char(50)  NULL ,
      MBR_FIRST_NAME  char(50)  NULL ,
      MBR_SEX char(1)  NULL , 
      SERVICE_DT char(11) NULL ,
      SERVICE_TYPE_CD char(3)  NULL ,
    );
    -------------------------
    -- Create BM3_HT_WT table
    -------------------------
    CREATE TABLE #BM3_HT_WT
    (
      MBR_LAST_NAME char(50)  NULL ,
      MBR_FIRST_NAME  char(50)  NULL ,
      MBR_SEX char(1)  NULL , 
      SERVICE_DT char(11) NULL ,
      SERVICE_TYPE_CD char(3)  NULL ,
    );
    ---------------------------
    -- Populate BM3 table
    ---------------------------
    INSERT INTO #BM3(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('SMITH', 'JOHN', 'M', '01-JUL-2017', 'BM3')
    ,('WAYNE', 'BRUCE', 'M', '03-JUL-2017', 'BM3')
    ,('LINCOLN', 'ABE', 'M', '05-JUL-2017', 'BM3')
    ,('BROWN', 'EMMET', 'M', '07-JUL-2017', 'BM3')
    ,('DOE', 'JANE', 'F', '10-JUL-2017', 'BM3')
    ,('LANE', 'LILY', 'F', '12-JUL-2017', 'BM3');
    -------------------------
    -- Populate BM3_HT_WT table
    -------------------------
    INSERT INTO #BM3_HT_WT(MBR_LAST_NAME, MBR_FIRST_NAME, MBR_SEX, SERVICE_DT, SERVICE_TYPE_CD)
    VALUES('SMITH', 'JOHN', 'M', '01-JUL-2017', 'HT')
    ,('SMITH', 'JOHN', 'M', '01-JUL-2017', 'WT')
    ,('SMITH', 'JOHN', 'M', '01-JUN-2017', 'HT')
    ,('SMITH', 'JOHN', 'M', '01-JUN-2017', 'WT')
    ,('WAYNE', 'BRUCE', 'M', '03-JUL-2017', 'HT')
    ,('WAYNE', 'BRUCE', 'M', '03-JUL-2017', 'WT')
    ,('WAYNE', 'BRUCE', 'M', '03-JUN-2017', 'HT')
    ,('WAYNE', 'BRUCE', 'M', '03-JUN-2017', 'HT')
    ,('LINCOLN', 'ABE', 'M', '05-JUL-2017', 'HT')
    ,('LINCOLN', 'ABE', 'M', '05-JUL-2017', 'WT')
    ,('LINCOLN', 'ABE', 'M', '05-JUN-2017', 'HT')
    ,('LINCOLN', 'ABE', 'M', '05-JUN-2017', 'HT')
    ,('BROWN', 'EMMET', 'M', '07-JUL-2017', 'HT')
    ,('BROWN', 'EMMET', 'M', '30-JUL-2017', 'WT')
    ,('BROWN', 'EMMET', 'M', '29-JUL-2017', 'WT')
    ,('BROWN', 'EMMET', 'M', '27-JUL-2017', 'HT')
    ,('DOE', 'JANE', 'F', '10-JUL-2017', 'HT')
    ,('DOE', 'JANE', 'F', '10-JUL-2017', 'WT')
    ,('DOE', 'JANE', 'F', '13-JUN-2017', 'HT')
    ,('DOE', 'JANE', 'F', '14-JUN-2017', 'WT')
    ,('LANE', 'LILY', 'F', '12-JUL-2017', 'HT')
    ,('LANE', 'LILY', 'F', '12-JUL-2017', 'WT')
    ,('LANE', 'LILY', 'F', '12-SEP-2017', 'HT')
    ,('LANE', 'LILY', 'F', '12-SEP-2017', 'WT');

    SELECT b.MBR_LAST_NAME
         , b.MBR_FIRST_NAME
         , b.MBR_SEX
         , b.SERVICE_DT
         , b.SERVICE_TYPE_CD
    FROM #BM3 b
    WHERE b.MBR_LAST_NAME IN (SELECT MBR_LAST_NAME FROM #BM3_HT_WT)
    AND b.MBR_FIRST_NAME IN (SELECT MBR_FIRST_NAME FROM #BM3_HT_WT)
    AND b.SERVICE_DT IN (SELECT SERVICE_DT FROM #BM3_HT_WT)
    UNION ALL
    SELECT bhw.MBR_LAST_NAME
         , bhw.MBR_FIRST_NAME
         , bhw.MBR_SEX
         , bhw.SERVICE_DT
         , bhw.SERVICE_TYPE_CD
    FROM #BM3_HT_WT bhw
    WHERE bhw.MBR_FIRST_NAME IN (SELECT MBR_FIRST_NAME FROM #BM3)
    AND bhw.MBR_LAST_NAME IN (SELECT MBR_LAST_NAME FROM #BM3)
    AND bhw.SERVICE_DT IN (SELECT SERVICE_DT FROM #BM3)
    ORDER BY MBR_FIRST_NAME, MBR_LAST_NAME, SERVICE_DT, SERVICE_TYPE_CD;
     DROP TABLE #BM3;
     DROP TABLE #BM3_HT_WT;
    GO

    select distinct b.MBR_FIRST_NAME, b.MBR_LAST_NAME, b.MBR_SEX, b.SERVICE_TYPE_CD, b.SERVICE_DT
    from dbo.BM3 b, dbo.BM3_HT_WT w
    where b.MBR_FIRST_NAME=w.MBR_FIRST_NAME
    and b.MBR_LAST_NAME=w.MBR_LAST_NAME
    and b.SERVICE_DT=w.SERVICE_DT
    union
    select distinct w.MBR_FIRST_NAME, w.MBR_LAST_NAME, w.MBR_SEX, w.SERVICE_TYPE_CD, w.SERVICE_DT
    from dbo.BM3 b, dbo.BM3_HT_WT w
    where b.MBR_FIRST_NAME=w.MBR_FIRST_NAME
    and b.MBR_LAST_NAME=w.MBR_LAST_NAME
    and b.SERVICE_DT=w.SERVICE_DT

  • Thank you for all the responses!  It turns out that Aditya Daruka's post did exactly what I ultimately wanted since I do want all the columns. 

    What I have learned is that I should have been doing a union instead of an inner join.  I have some more homework to do!

    -Mark
    MSSQL 2019 Standard, Azure Hosted. Techie/Sysadmin by trade; Three years as a "DBA" now.

  • usererror - Thursday, August 17, 2017 10:12 AM

    Thank you for all the responses!  It turns out that Aditya Daruka's post did exactly what I ultimately wanted since I do want all the columns. 

    What I have learned is that I should have been doing a union instead of an inner join.  I have some more homework to do!

    A couple of things -
    You really did a great job of posting the DDL, inserts, what you have tried, etc. That really helps and is likely part of the reason why you got all those responses. And thanks for posting back what worked. It can be frustrating when people post a few things to try and the person posting the question never comes back and says anything. They just kind of disappear and you never know what worked.
    And most importantly - I really like the name you are using!! 🙂

    Sue

  • Sue_H - Thursday, August 17, 2017 10:48 AM

    A couple of things -
    You really did a great job of posting the DDL, inserts, what you have tried, etc. That really helps and is likely part of the reason why you got all those responses. And thanks for posting back what worked. It can be frustrating when people post a few things to try and the person posting the question never comes back and says anything. They just kind of disappear and you never know what worked.
    And most importantly - I really like the name you are using!! 🙂

    Sue

    Thanks, Sue!  I am trying to follow the directions for posting that were in the 'new user tips' area.  This is a whole new world to me.  I am really enjoying it, but at times it's really frustrating.  My SQL book I bought that is very good and has been helpful to get the very basic stuff, but the rest I am only going to learn from experience and others and hopefully a class or two down the road...

    And there is a lot of 'user error' right now, hence the name. 🙂

    -Mark
    MSSQL 2019 Standard, Azure Hosted. Techie/Sysadmin by trade; Three years as a "DBA" now.

  • usererror - Thursday, August 17, 2017 2:55 PM

    Sue_H - Thursday, August 17, 2017 10:48 AM

    A couple of things -
    You really did a great job of posting the DDL, inserts, what you have tried, etc. That really helps and is likely part of the reason why you got all those responses. And thanks for posting back what worked. It can be frustrating when people post a few things to try and the person posting the question never comes back and says anything. They just kind of disappear and you never know what worked.
    And most importantly - I really like the name you are using!! 🙂

    Sue

    Thanks, Sue!  I am trying to follow the directions for posting that were in the 'new user tips' area.  This is a whole new world to me.  I am really enjoying it, but at times it's really frustrating.  My SQL book I bought that is very good and has been helpful to get the very basic stuff, but the rest I am only going to learn from experience and others and hopefully a class or two down the road...

    And there is a lot of 'user error' right now, hence the name. 🙂

    Hey, you're trying and that matters a lot.  It is a whole new world, so welcome to it.  There's a lot of learn in the world of databases and I don't know anyone who knows everything, especially me. 😉

    If you're interested in reading some series of articles that take you from beginner and progress from there, check out the "Stairways" link in the navigation section on this site.  There's some really good ones there.
    There are also a number of books available to download for free in PDF form that should keep you busy for quite a while.
    Last, the posts to some of the questions on this site contain a lot of hidden gems.  It's the discussion on this site that I think makes the real difference.  A seemingly simple question can have multiple solutions, but when some of the heavy hitters on this site get to playing with it, others are discovered and the best ones usually make themselves evident.  Enjoy! 

    Oh yeah...Welcome to SSC.

Viewing 8 posts - 1 through 7 (of 7 total)

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