Help with Left Join

Carlton Patterson 761 Reputation points
2022-06-28T20:28:51.033+00:00

Hello Community,

Can someone help with a left join query

I would like to do a left join between table one

CREATE TABLE tableone (  
  
Id varchar(50),  
  
SinkCreatedOn datetime2,  
  
SinkModifiedOn datetime2,  
  
statecode int,  
  
statuscode int,  
  
ts_primarysecondaryfocus varchar(255),  
  
customertypecode varchar(255),  
  
address1_addresstypecode varchar(255),  
  
accountclassificationcode int,  
  
ts_easeofworking varchar(255),  
  
ts_ukrow varchar(255),  
  
preferredappointmenttimecode varchar(255),  
  
ts_address1addresstype int,  
  
xpd_relationshipstatus int,  
  
ts_relationship varchar(255))  
  
INSERT tableone VALUES  
  
('0bf6d52f-707f-e811-a95c-00224800cc97',CONVERT(DATETIME2, '2022-05-19 16:25:52.0000000', 121),CONVERT(DATETIME2, '2022-05-19 16:25:52.0000000', 121),0,1,'','','',1,'','','',717750001,NULL,''),  
  
('0df6d52f-707f-e811-a95c-00224800cc97',CONVERT(DATETIME2, '2022-05-19 16:25:52.0000000', 121),CONVERT(DATETIME2, '2022-05-19 16:25:52.0000000', 121),0,1,'','','',1,'','','',717750001,NULL,''),  
  
('0ff6d52f-707f-e811-a95c-00224800cc97',CONVERT(DATETIME2, '2022-05-19 16:25:52.0000000', 121),CONVERT(DATETIME2, '2022-05-19 16:25:52.0000000', 121),0,1,'','','',1,'','','',717750001,NULL,''),  
  
('11f6d52f-707f-e811-a95c-00224800cc97',CONVERT(DATETIME2, '2022-05-19 16:25:52.0000000', 121),CONVERT(DATETIME2, '2022-05-19 16:25:52.0000000', 121),0,1,'','','',1,'','','',717750001,NULL,''),  
  
('c4a24708-0989-e811-a95d-00224800cc97',CONVERT(DATETIME2, '2022-05-19 16:22:37.0000000', 121),CONVERT(DATETIME2, '2022-05-19 16:22:37.0000000', 121),0,1,'','','',1,'','','',717750001,930580000,''),  
  
('c6a24708-0989-e811-a95d-00224800cc97',CONVERT(DATETIME2, '2022-05-19 16:22:37.0000000', 121),CONVERT(DATETIME2, '2022-05-19 16:22:37.0000000', 121),0,1,'','','',1,'','','',717750001,NULL,''),  
  
('c8a24708-0989-e811-a95d-00224800cc97',CONVERT(DATETIME2, '2022-05-19 16:22:37.0000000', 121),CONVERT(DATETIME2, '2022-05-19 16:22:37.0000000', 121),0,1,'','','',1,'','','',717750001,NULL,''),  
  
('caa24708-0989-e811-a95d-00224800cc97',CONVERT(DATETIME2, '2022-05-19 16:22:37.0000000', 121),CONVERT(DATETIME2, '2022-05-19 16:22:37.0000000', 121),0,1,'','','',1,'','','',717750001,NULL,''),  
  
('cca24708-0989-e811-a95d-00224800cc97',CONVERT(DATETIME2, '2022-05-19 16:22:37.0000000', 121),CONVERT(DATETIME2, '2022-05-19 16:22:37.0000000', 121),0,1,'','','',1,'','','',717750001,NULL,'')  
  
SELECT * FROM tableone  

And table two

CREATE TABLE tabletwo (  
  
participationtypemask varchar(50),  
  
instancetypecode varchar(50),  
  
donotpostalmail varchar(50),  
  
donotfax varchar(50),  
  
donotphone varchar(50),  
  
ispartydeleted varchar(50),  
  
donotemail varchar(50),  
  
ts_primarysecondaryfocus varchar(50),  
  
customertypecode varchar(50),  
  
address1_addresstypecode varchar(50),  
  
accountclassificationcode varchar(50),  
  
ts_ukrow varchar(50),  
  
preferredappointmenttimecode varchar(50),  
  
address2_freighttermscode varchar(50))  
  
INSERT tabletwo VALUES  
  
('Sender','Not Recurring','Allow','Allow','Allow','No','Allow','Tier 1','Competitor','Bill To','Default Value','UK','Morning','Default Value'),  
  
('To Recipient','Recurring Master','Do Not Allow','Do Not Allow','Do Not Allow','Yes','Do Not Allow','Tier 2','Consultant','Ship To',NULL,'RoW','Afternoon',NULL),  
  
('CC Recipient','Recurring Instance',NULL,NULL,NULL,NULL,NULL,'TBC','Customer','Primary',NULL,NULL,'Evening',NULL),  
  
('BCC Recipient','Recurring Exception',NULL,NULL,NULL,NULL,NULL,NULL,'Investor','Other',NULL,NULL,NULL,NULL),  
  
('Required attendee','Recurring Future Exception',NULL,NULL,NULL,NULL,NULL,NULL,'Partner',NULL,NULL,NULL,NULL,NULL),  
  
('Optional attendee',NULL,NULL,NULL,NULL,NULL,NULL,NULL,'Influencer',NULL,NULL,NULL,NULL,NULL),  
  
('Organizer',NULL,NULL,NULL,NULL,NULL,NULL,NULL,'Press',NULL,NULL,NULL,NULL,NULL),  
  
('Regarding',NULL,NULL,NULL,NULL,NULL,NULL,NULL,'Prospect',NULL,NULL,NULL,NULL,NULL)  
  
SELECT * FROM tabletwo  
  
  

I would like to do the left join on fields:

ts_primarysecondaryfocus

ts_ukrow

The expected output should look something like the following:

215758-result.png

Someone suggested the following, but it didn't work

SELECT a.<field>, b.<field>...  
  
FROM Table1 a  
  
LEFT JOIN Table1 b  
  
ON a.PK = b.FK  
  
WHERE b.OtherColumn IS NULL  

Any help greatly appreciated

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
0 comments No comments
{count} votes

Answer accepted by question author
  1. LiHong-MSFT 10,061 Reputation points
    2022-06-29T09:20:33.11+00:00

    Hi @Carlton Patterson
    How about add a Row_number column before you Left Join the two tables. Check this:

    ;WITH CTE1 AS  
    (  
     SELECT *,ROW_NUMBER()OVER(ORDER BY ts_primarysecondaryfocus)RowNum FROM tableone  
    ),CTE2 AS  
    (  
     SELECT *,ROW_NUMBER()OVER(ORDER BY ts_primarysecondaryfocus)RowNum FROM tabletwo  
    )  
    SELECT C1.Id,C1.SinkCreatedOn,C1.SinkModifiedOn,C1.statecode,C1.statuscode  
          ,CASE WHEN C1.ts_primarysecondaryfocus<>ISNULL(C2.ts_primarysecondaryfocus,'')THEN C2.ts_primarysecondaryfocus ELSE C1.ts_primarysecondaryfocus END AS ts_primarysecondaryfocus  
    	  ,C1.customertypecode,C1.address1_addresstypecode,C1.accountclassificationcode,C1.ts_easeofworking   
    	  ,CASE WHEN C1.ts_ukrow<>ISNULL(C2.ts_ukrow,'')THEN C2.ts_ukrow ELSE C1.ts_ukrow END AS ts_ukrow   
    	  ,C1.preferredappointmenttimecode,C1.xpd_relationshipstatus,C1.ts_relationship  
    FROM CTE1 C1 LEFT JOIN CTE2 C2 ON C1.RowNum = C2.RowNum  
    

    Best regards,
    LiHong

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Erland Sommarskog 128.9K Reputation points MVP Volunteer Moderator
    2022-06-28T21:39:44.927+00:00

    Looks like an uphill battle to me. I can't see anything in common between these tables that would permit a join. You say that you want to join on the columns ts_primarysecondaryfocus and ts_ukrow. But in tableone, these columns are blank throughout and in tabletwo a few rows have value, and the rest are NULL.

    I saw that you posted an image of an expected result, but there is no description of why you want that result.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.