Microsoft Access Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft Access Help

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 ASP Free Forums Sponsor:
  #1  
Old November 10th, 2009, 04:30 PM
td04 td04 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 4 td04 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 48 sec
Reputation Power: 0
Queries - Counting in a query

I am having difficulty with creating a query. My tables are listed below

Patient(PatientID, LName, FName, Company ID)
Insurance(Company ID, COmpany Name)
Doctor(Docor ID, LName, FName)
Visit(Doctor ID, Patient ID, Visit Date, VIsit time, Visit Duration)

I need to create a query that when I input the Doctor_ID it will list the insurance companies that the doctor deals with organized in ascending order based on the companies name and number of customers insured by the company. Each company can only show up in the query once. I can't get it to count correctly.

I created the query then tried to sort by count for patients but it gives me 3 and 2 patients for each company but there are only 3 patients total not 5.
Thanks for your help

Reply With Quote
  #2  
Old November 10th, 2009, 05:34 PM
June7 June7 is offline
Contributing User
Click here for more information.
 
Join Date: Apr 2009
Location: The Great Land
Posts: 1,118 June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 35 m 17 sec
Reputation Power: 240
Use Access query builder to build the query with joins to appropriate tables and the needed fields. Then switch to the SQL window and add the DISTINCT keyword. Ex:

SELECT DISTINCT [Company ID], ....

If still have problem, post the entire query for analysis.

Reply With Quote
  #3  
Old November 10th, 2009, 05:38 PM
td04 td04 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 4 td04 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 48 sec
Reputation Power: 0
Quote:
Originally Posted by June7
Use Access query builder to build the query with joins to appropriate tables and the needed fields. Then switch to the SQL window and add the DISTINCT keyword. Ex:

SELECT DISTINCT [Company ID], ....

If still have problem, post the entire query for analysis.


thanks for your help so far. I don't understand SQL as this is for a class I am taking here is the SQL that is for my query. I still havent added the count function into the query below.

SELECT InsuranceCompany.Company_Name, Patient.Patient_ID
FROM (InsuranceCompany INNER JOIN Patient ON InsuranceCompany.[Company_ID] = Patient.[Company_ID]) INNER JOIN (Doctor INNER JOIN Visit ON Doctor.Doctor_ID = Visit.Doctor_ID) ON Patient.Patient_ID = Visit.Patient_ID
GROUP BY InsuranceCompany.Company_Name, Doctor.Doctor_ID, Patient.Patient_ID
HAVING (((Doctor.Doctor_ID)=[Enter Doctor ID]));

Reply With Quote
  #4  
Old November 10th, 2009, 05:48 PM
June7 June7 is offline
Contributing User
Click here for more information.
 
Join Date: Apr 2009
Location: The Great Land
Posts: 1,118 June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 35 m 17 sec
Reputation Power: 240
You need to do an aggregate calc, might not need the DISTINCT keyword with GroupBy on Company. Try this:
Code:
SELECT  InsuranceCompany.Company_Name, Count(Patient.Patient_ID) As PatientCount
FROM (InsuranceCompany INNER JOIN Patient ON InsuranceCompany.[Company_ID] = Patient.[Company_ID]) INNER JOIN (Doctor INNER JOIN Visit ON Doctor.Doctor_ID = Visit.Doctor_ID) ON Patient.Patient_ID = Visit.Patient_ID
GROUP BY InsuranceCompany.Company_Name,
HAVING (((Doctor.Doctor_ID)=[Enter Doctor ID]));

Last edited by June7 : November 10th, 2009 at 05:51 PM.

Reply With Quote
  #5  
Old November 10th, 2009, 06:04 PM
td04 td04 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 4 td04 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 48 sec
Reputation Power: 0
Quote:
Originally Posted by June7
You need to do an aggregate calc, might not need the DISTINCT keyword with GroupBy on Company. Try this:
Code:
SELECT  InsuranceCompany.Company_Name, Count(Patient.Patient_ID) As PatientCount
FROM (InsuranceCompany INNER JOIN Patient ON InsuranceCompany.[Company_ID] = Patient.[Company_ID]) INNER JOIN (Doctor INNER JOIN Visit ON Doctor.Doctor_ID = Visit.Doctor_ID) ON Patient.Patient_ID = Visit.Patient_ID
GROUP BY InsuranceCompany.Company_Name,
HAVING (((Doctor.Doctor_ID)=[Enter Doctor ID]));


I pasted that into the SQL view and it says there was a syntax error in the group by clause

Reply With Quote
  #6  
Old November 10th, 2009, 06:16 PM
June7 June7 is offline
Contributing User
Click here for more information.
 
Join Date: Apr 2009
Location: The Great Land
Posts: 1,118 June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 35 m 17 sec
Reputation Power: 240
Sorry, looks I left a comma in place. Delete the comma just before HAVING.

Reply With Quote
  #7  
Old November 10th, 2009, 06:19 PM
td04 td04 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 4 td04 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 48 sec
Reputation Power: 0
Quote:
Originally Posted by June7
Sorry, looks I left a comma in place. Delete the comma just before HAVING.


thanks for your help so far but now it says:

You tried to execute a theory that does not include the specified expression 'Doctor.Doctor_ID=[Enter Doctor ID]' as part of an aggregate function

Reply With Quote
  #8  
Old November 10th, 2009, 08:25 PM
June7 June7 is offline
Contributing User
Click here for more information.
 
Join Date: Apr 2009
Location: The Great Land
Posts: 1,118 June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level)June7 User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 35 m 17 sec
Reputation Power: 240
The HAVING clause is a paste of your original query. I had assumed it was working for you. Don't think I have ever used HAVING clause. Do you want to use this query in a report? I would eliminate the HAVING clause and use the WHERE clause of the DoCmd.OpenReport method to filter the records. Is the doctor ID input to a box on form by user and does that same form call the report to open? Then:
DoCmd.OpenReport "reportname", ,"Doctor_ID=" & Me.IDtextboxName

And you could let the report grouping/sorting features do the heavy lifting instead of trying to design the query. Are you using Access2007?

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft Access Help > Queries - Counting in a query


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





 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

 

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





© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 10 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek