I have a problem where I wish to take all the rows in a table with col5=2206
and then select all other rows with col2 and col3 containing the same combination of col2 and col3 as that of col5 (fi. in the example below col2=2, col3=6)
I also wish to filter the returned rows to those with certain values in col5 (fi. col5=4000).
('101', '2', '6', '2009-12-31', '2206', 'Exempt', '0', '0', '0', '4', '5'),
('102', '2', '6', '2009-12-31', '4000', 'Exempt', '-1', '0', '0', '4', '5'),
('103', '2', '6', '2009-12-31', '1200', '', '1', '0', '0', '4', '5');
I have tried various sub query statements. But can't get anything to work as on one table. Is this possible or do I need to create a bigger script.
You use self joins to achieve what you want.
Select t2.*
From table t1
Join table t2 on ( t2.col2 = t1.col2 and t2.col3 = t1.col3 )
Where t1.col5 = '2206'
And t2.col5 <> '2206' -- replace that with specific filters like t2.col5 = '4000'
;
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments