Hi @Tom Sanderson ,
Thank you so much for posting here.
We recommend that you post CREATE TABLE statements for your tables(table1 and table2) together with INSERT statements with sample data. We also need to see the expected result of the sample and more details about the logic of table3 like adding '(' and ')'.
You could also refer below example and check whether it is helpful to you.
DROP TABLE IF EXISTS table1
DROP TABLE IF EXISTS table2
DROP TABLE IF EXISTS table3
create table table1
(
PayeeReferrer varchar(10),
AccidentType varchar(10),
ExpertType varchar(10),
Amount int
)
create table table2
(
CaseNo int,
PayeeReferrer varchar(10),
AccidentType varchar(10),
ExpertType varchar(10)
)
create table table3
(
CaseNo int,
Amount int
)
insert into table1 values
('PRA','ATB','ETC',10),
('PRB','ATB','ETD',20),
('PRC','ATB','ETC',30),
('PRD','ATC','ETD',40),
('PRE','ATC','ETC',50),
('PRF','ATC','ETD',60),
('PRG','DEFUALT','DEFUALT',70)
insert into Table2 values
(1,'PRA','ATB','ETC'),
(2,'PRB','ATB','ETD'),
(3,'PRC','ATC','ETC'),
(4,'PRD','ATB','ETD'),
(5,'PRE','ATC','ETE'),
(6,'PRF','ATC','ETD'),
(7,'PRG','ATE','ETF')
INSERT INTO table3
SELECT B.CaseNo,A.Amount
FROM table1 A,Table2 B
WHERE
A.PayeeReferrer=B.PayeeReferrer AND
(
(A.AccidentType=B.AccidentType AND A.ExpertType=B.ExpertType)
OR
(A.AccidentType=B.AccidentType)
OR
(A.ExpertType=B.ExpertType)
OR
(A.AccidentType = 'DEFUALT' AND A.ExpertType = 'DEFUALT')
)
SELECT * FROM table3
Output:
CaseNo Amount
1 10
2 20
3 30
4 40
5 50
6 60
7 70
Best regards
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Hot issues November--What can I do if my transaction log is full?
Hot issues November--How to convert Profiler trace into a SQL Server table