Looking to use the following data and populate the datetime in status table only if status =
‘D’ and there is NOT another record with the same transaction_id that has a status <> ‘D’, if the ‘D’ record is the selected record, we also want the datetime of the first ‘D’ record.
DECLARE decision TABLE (
transaction_id NCHAR(1),
event_id INT,
status NCHAR(1) NULL,
statud_date datetime
)
INSERT decision VALUES
( '1' , 1 , 'D', '2011-01-01'),
( '1' , 2 , 'D', '2011-01-01'),
( '1' , 3 , 'A', '2011-01-01'),
( '2' , 1 , 'D', '2011-05-01'),
( '2' , 2 , 'D', '2011-05-02'),
( '2' , 3 , 'D', '2011-05-03'),
( '3' , 1 , 'D', '2011-05-05'),
( '3' , 2 , 'A', '2011-05-06'),
( '3' , 3 , 'C', '2011-05-06'),
( '4' , 1 , 'D', '2011-10-01')
DECLARE status TABLE (
transaction_id NCHAR(1),
default_dt datetime
)
INSERT load VALUES
( '1' , NULL ),
( '2' , NULL ),
( '3' , NULL ),
( '4' , NULL )
Looking to get this result:
1 NULL
2 2011-05-01
3 NULL
4 2011-10-01
If I understand you correct tnan you can look at this: