Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Complicated Select :(

Author  Topic 

aguazul
Starting Member

3 Posts

Posted - 2009-09-14 : 14:55:34
I think I bit off more than I could chew. I've pretty much taught myself SQL and doing quite well with it. But, I am having trouble working this query out.

I have 2 tables: AmpPha_DB and AmpPha_Patients
Each table has similar columns: Last, First, Birthdate, Visitdate, AmpOD, AmpOS, PhaOD, PhaOS

I am basically trying to find the records where the AmpOD, AmpOS, PhaOD, PhaOS do not match up correctly in both tables for each patient's visit.

I am currently using this bulky code:

SELECT AmpPha_DB.BP, AmpPha_DB.Last, AmpPha_DB.First, AmpPha_DB.Birth, AmpPha_DB.Visit, AmpPha_DB.[od amp], Round(AmpPha_Patients.AMPDX,2) AS AMPDX, AmpPha_DB.[od pha], Round(AmpPha_Patients.PHDX,2) AS PHDX, AmpPha_DB.[os amp], Round(AmpPha_Patients.AMPSX,2) AS AMPSX, AmpPha_DB.[os pha], Round(AmpPha_Patients.PHSX,2) AS PHSX

FROM AmpPha_DB
INNER JOIN AmpPha_Patients
ON (AmpPha_DB.Visit = AmpPha_Patients.EXAMDATE)
AND (AmpPha_DB.Birth = AmpPha_Patients.BIRTHDATE)
AND (AmpPha_DB.Last = AmpPha_Patients.LASTNAME)

WHERE (((AmpPha_DB.[od amp])<>Round([AmpPha_Patients].[AMPDX],2)))
OR (((AmpPha_DB.[od pha])<>Round([AmpPha_Patients].[PHDX],2)))
OR (((AmpPha_DB.[os amp])<>Round([AmpPha_Patients].[AMPSX],2)))
OR (((AmpPha_DB.[os pha])<>Round([AmpPha_Patients].[PHSX],2)))
ORDER BY AmpPha_DB.Last;

So, it searches both tables and joins the data based on the patients Lastname, Birthdate, and Visitdate. This works fine for the most part in finding errors between the two tables. But, for patients who had 2 recordings on the same day it shows an error when there isn't really one because it is comparing the data collected on the same day and seeing that they are different.

What I want is a query that will find the records in AmpPha_DB that do not have a match in AmpPha_Patients using the Lastname, Birthdate, Visitdate, AmpOD, AmpOS, PhaOD, and PhaOS. So, the record only gets returned if it doesn't exist in both tables.

It is a difficult problem to describe, but I tried my best :P
Thanks for your help,
Brandon

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-09-14 : 16:37:19
quote:
So, the record only gets returned if it doesn't exist in both tables.

I hope that was a typo. . If you want to show rows that are in one table but not another table then:

select a.*
from <tableWithAllRows> a
Left outer join <tableWithMissingRows> m
on m.<all columns to correlate on> = a.<all columns to correlate on>
where m.<any correlated column> is NULL

Like this:

select d.*
FROM AmpPha_DB d
left outer join AmpPha_Patients p
ON d.Visit = p.EXAMDATE
AND d.Birth = p.BIRTHDATE
AND d.Last = p.LASTNAME
and d.[od amp] = Round(p.[AMPDX],2)
and d.[od pha] = Round(p.[PHDX],2)
and d.[os amp] = Round(p.[AMPSX],2)
and d.[os pha] = Round(p.[PHSX],2)

where p.examdate is null

ORDER BY d.Last




Be One with the Optimizer
TG
Go to Top of Page

aguazul
Starting Member

3 Posts

Posted - 2009-09-15 : 12:48:09
Wow thanks :) It works

It is even finding other inconsistencies that I would have probably overlooked

Is there any way to get the actual values that are in table Patients (p) to show up in the result set so that I can compare the data that is entered into both tables and see why there was an inconsistency?

I tried:
select d.*, p.*
FROM AmpPha_DB d
left outer join AmpPha_Patients p
etc...

but that of course didn't work because the query is designed to find null fields in p.

Any hints? :P
Thanks again for your response and taking the time to look at my ugly code.. you made it looks much nicer by using AS d and AS p

.b.
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-09-15 : 13:24:36
Maybe this. It will show only rows that are unique - meaning there is some difference between the tables or rows are missing from one table. So it will hide only rows that are identical between the tables:

select min(sourceTbl) as sourceTbl
,d.Visit
,d.Birth
,d.Last
,d.[od amp]
,d.[od pha]
,d.[os amp]
,d.[os pha]
from (
select 'AmpPha_DB' as sourceTbl
,d.Visit
,d.Birth
,d.Last
,d.[od amp]
,d.[od pha]
,d.[os amp]
,d.[os pha]
from AmpPha_DB
UNION ALL
select 'AmpPha_Patients'
,d.Visit = p.EXAMDATE
,p.BIRTHDATE
,p.LASTNAME
,Round(p.[AMPDX],2)
,Round(p.[PHDX],2)
,Round(p.[AMPSX],2)
,Round(p.[PHSX],2)
from AmpPha_Patients
) d
group by d.Visit
,d.Birth
,d.Last
,d.[od amp]
,d.[od pha]
,d.[os amp]
,d.[os pha]
having count(*) = 1
order by 2,3,4,1


Be One with the Optimizer
TG
Go to Top of Page

aguazul
Starting Member

3 Posts

Posted - 2009-09-15 : 16:44:53
woah.. I would have never came up with that :P Very complex.

When I run this query now I get an error:
"Circular reference caused by alias 'sourceTbl' in query defenition's SELECT list."

I'm using MS Access 2000.


.b.
Go to Top of Page
   

- Advertisement -