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.
| 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_PatientsEach table has similar columns: Last, First, Birthdate, Visitdate, AmpOD, AmpOS, PhaOD, PhaOSI 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 PHSXFROM 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 :PThanks 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> aLeft outer join <tableWithMissingRows> m on m.<all columns to correlate on> = a.<all columns to correlate on>where m.<any correlated column> is NULLLike this:select d.*FROM AmpPha_DB dleft outer join AmpPha_Patients pON d.Visit = p.EXAMDATEAND d.Birth = p.BIRTHDATEAND d.Last = p.LASTNAMEand 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 nullORDER BY d.Last Be One with the OptimizerTG |
 |
|
|
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 dleft outer join AmpPha_Patients petc...but that of course didn't work because the query is designed to find null fields in p.Any hints? :PThanks 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. |
 |
|
|
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 ) dgroup by d.Visit ,d.Birth ,d.Last ,d.[od amp] ,d.[od pha] ,d.[os amp] ,d.[os pha]having count(*) = 1order by 2,3,4,1 Be One with the OptimizerTG |
 |
|
|
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. |
 |
|
|
|
|
|
|
|