MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 17th, 2012, 12:02 PM
erikz erikz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 6 erikz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 46 m 31 sec
Reputation Power: 0
Complex SQL Query on Multiple Tables

I have three tables.

Patients
PatientNotes
Invoices

Zero or One patient notes record can exist for any patient. Related column is Id (Patient Id)

Zero, One or Many invoices can exist for a single patient. Related column is Id (Patient Id)

The patient's current name (last,first) is stored in the patient record and the name at the time of service is stored in each invoice record. Therefore the name on the last invoice is stored in the patient record.

I am able to do a left join on the notes table with the patients to list patients with or without notes. If there are notes for the patient the notes detail is returned, else the query returns null for the notes data and this is perfect.

Here's my requirement: We are looking for AKA's (Also Known As). With each patient record, I need to list the first (or ANY) invoice where the patient's name may be different from the current name i.e from marriage, name change, error etc... If the names on all of the invoices match the patient names exactly then just return null.

The query results should look something like this with one row for each patient (Additional columns ignored for simplicity here):

PtLast,PtFirst,PtId,PtNotesYN,InvDate,InvLast,InvFirst,InvNumber

Thanks is advance,
Erik

Reply With Quote
  #2  
Old July 18th, 2012, 04:56 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,351 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 8 h 1 m 50 sec
Reputation Power: 390
Code:
select Patients.PtLast,
       Patients.PtFirst,
       Patients.PtId,
       PatientNotes.PtNotesYN,
       Invoices.InvDate,
       Invoices.InvLast,
       Invoices.InvFirst,
       Invoices.InvNumber
  from patients
  left
  join PatientNotes 
    on Patients.PtId = PatientNotes.PtId
  left
  join invoices
    on Patients.PtId = Invoices.PtId
   and (Patients.PtLast <> Invoices.InvLast
    or Patients.PtFirst <> Invoices.InvFirst)


If a patient have done multiple name changes there will be one row for each change.

Reply With Quote
  #3  
Old July 18th, 2012, 10:38 AM
erikz erikz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 6 erikz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 46 m 31 sec
Reputation Power: 0
Thanks for the quick reply and I will give this a try. I can filter out the duplicate results for multiple name changes when displaying to the UI so that will not be an issue. However, is there a way to do that in the query?

Reply With Quote
  #4  
Old July 18th, 2012, 10:41 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,351 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 8 h 1 m 50 sec
Reputation Power: 390
Code:
select Patients.PtLast,
       Patients.PtFirst,
       Patients.PtId,
       PatientNotes.PtNotesYN,
       Invoices.InvDate,
       Invoices.InvLast,
       Invoices.InvFirst,
       Invoices.InvNumber
  from patients
  left
  join PatientNotes 
    on Patients.PtId = PatientNotes.PtId
  left
  join (select min(Invoices.InvDate) as InvDate,
               Invoices.InvLast,
               Invoices.InvFirst,
               Invoices.PtId,
               min(Invoices.InvNumber) as InvNumber
          from invoices
         group
            by Invoices.InvLast,
               Invoices.InvFirst,
               Invoices.PtId) as invoices 
    on Patients.PtId = Invoices.PtId
   and (Patients.PtLast <> Invoices.InvLast
    or Patients.PtFirst <> Invoices.InvFirst)

Reply With Quote
  #5  
Old July 18th, 2012, 11:08 AM
erikz erikz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 6 erikz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 46 m 31 sec
Reputation Power: 0
Thank you!

Reply With Quote
  #6  
Old July 19th, 2012, 07:49 AM
erikz erikz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 6 erikz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 46 m 31 sec
Reputation Power: 0
One more thought here...

When use the query that yields one row for each invoice on a name change I get what I expect. However, when I use the second query, I run into an error "msg 207, level 16, line # Invalid column name" when I include any column after the fifth column in the invoices table. Do you know what might cause this?

Reply With Quote
  #7  
Old July 19th, 2012, 08:18 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,351 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 8 h 1 m 50 sec
Reputation Power: 390
It is due to the fact that only those columns present in the derived table can be selected in the outmost select list. If you want to select further columns from the invoice table these must be included in the select list of the derived table.

The select list of the derived table will depend on what data you have when there are multiple invoices with the same name change. For instance, would you like to retrive all data from the invoice with the lowest date within that group? You need to describe how this should be assorted.

Reply With Quote
  #8  
Old July 19th, 2012, 09:40 AM
erikz erikz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 6 erikz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 46 m 31 sec
Reputation Power: 0
Thanks again. I hate to keep asking questions and taking up your time. Can you recommend any comprehensive resources on the web (or in print) for learning SQL in depth. I've been through some basic tutorials etc... but I think that I am going to need more than is available on the basic self help websites that are out there.

Erik

Quote:
Originally Posted by swampBoogie
It is due to the fact that only those columns present in the derived table can be selected in the outmost select list. If you want to select further columns from the invoice table these must be included in the select list of the derived table.

The select list of the derived table will depend on what data you have when there are multiple invoices with the same name change. For instance, would you like to retrive all data from the invoice with the lowest date within that group? You need to describe how this should be assorted.

Reply With Quote
  #9  
Old July 23rd, 2012, 11:24 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,351 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 8 h 1 m 50 sec
Reputation Power: 390
There is books on line for SQL server

http://msdn.microsoft.com/en-us/library/bb510741.aspx

For advanced SQL, look for books by Joe Celko.

For books on less than advanced I don't have any recommendations.

Reply With Quote
  #10  
Old July 23rd, 2012, 12:22 PM
erikz erikz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 6 erikz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 46 m 31 sec
Reputation Power: 0
Thanks!

Erik

Reply With Quote
  #11  
Old July 23rd, 2012, 02:41 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,375 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 30 m 23 sec
Reputation Power: 4140
Quote:
Originally Posted by swampBoogie
For books on less than advanced I don't have any recommendations.
i can recommend my book, it's aimed at beginners and will move them to intermediate, and it's vendor-agnostic (but does mention mysql more than others in the sidebars)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Complex SQL Query on Multiple Tables

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap