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:
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