Pregunta

I have two Tables as below..

  1. tbPatientEncounter
  2. tbVoucher

when i execute select query as below

Select EncounterIDP,EncounterNumber from tbPatientEncounter

enter image description here

it returens me 180 rows. and

Select VoucherIDP,EncounterIDF from tbVoucher

enter image description here

above query returns me 165 rows.

but i want to execute select query that returns me data like EncounterIDP not in tbVoucher, for that i have tried below Select query...

    Select * from tbPatientEncounter pe 
    where pe.EncounterIDP not in 
    (Select v.EncounterIDF from tbVoucher v )

it doesn't returns any row. in first image it shows EncounterIDP 9 in tbPatientEncounter, but it not inserted in tbVoucher for that i have tried select Query like

Select * from tbVoucher where EncounterIDF = 9 

it returns me 0 rows.

My question is what is wrong with my above Not In Query.?

¿Fue útil?

Solución

In all likelihood, the problem is NULL values in tbVoucher. Try this:

Select *
from tbPatientEncounter pe 
where pe.EncounterIDP not in (Select v.EncounterIDF
                              from tbVoucher v
                              where v.EncounterIDF is not NULL
                             )

Otros consejos

Are you comparing the correct fields in tbVoucher?

Try using a left join

Select EncounterIDP,EncounterNumber from tbPatientEncounter
       left join tbVoucher on EncounterIDP = EncounterIDF
where EncounterIDF is null

Call me a skeptic because I don't see anything wrong with your query. Is this really all in the query or did you simplify it for us?

Select * from tbPatientEncounter pe 
where pe.EncounterIDP not in 
(Select v.EncounterIDF from tbVoucher v )
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top