Hi,
In a trasaction table there are some erroneous records in a column called Policynumber that need to be indentified. There is a column called Tran type which can have values "A" and "D". For every record with tran type 'A' there will be a corresponding record with tran_typ "D". So we wanted to check whether the errorneous policy records have matching "A" records and "D" records.
We used the following Self Join.
"Select count(*) from Tran_table A, Tran_table B
where A.tran_typ = 'A' and A.Polnum like '%,%'
and B.tran_typ = 'D' and B.Polnum like '%,%'
and A.POlnum = B.polnum
It gave a result as 10482
But we used another query (correlated sub query)
Select count(*) from Tran_table B
where
b.polnum in (
select polnum from tran_table A
where A.tran_typ = 'A' and A.Polnum like '%,%'
)
and
b.Tran_typ = 'D'
This query gave a result as "9048"
When we analysed the table , the second result is correct. as we had 9048 "A" records, 9071 D records. And we found that 23 "D" records does not have matching "A" records.
We are not sure what was the error in the first query. Why did we get a wierd result. ?
In a trasaction table there are some erroneous records in a column called Policynumber that need to be indentified. There is a column called Tran type which can have values "A" and "D". For every record with tran type 'A' there will be a corresponding record with tran_typ "D". So we wanted to check whether the errorneous policy records have matching "A" records and "D" records.
We used the following Self Join.
"Select count(*) from Tran_table A, Tran_table B
where A.tran_typ = 'A' and A.Polnum like '%,%'
and B.tran_typ = 'D' and B.Polnum like '%,%'
and A.POlnum = B.polnum
It gave a result as 10482
But we used another query (correlated sub query)
Select count(*) from Tran_table B
where
b.polnum in (
select polnum from tran_table A
where A.tran_typ = 'A' and A.Polnum like '%,%'
)
and
b.Tran_typ = 'D'
This query gave a result as "9048"
When we analysed the table , the second result is correct. as we had 9048 "A" records, 9071 D records. And we found that 23 "D" records does not have matching "A" records.
We are not sure what was the error in the first query. Why did we get a wierd result. ?
Comment