Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Access SQL statement using DISTINCT
Jerry 

16 years ago
Hello, thnaks for having a look.

I have worked this query a number of ways but just can't get wnat I want.  3 Columns (veterinary):  
ClinicName, PatientID, Amount

I'm trying to SUM the Amount, COUNT the DISTINCT PatientIDs and group them by ClinicName.  Sounds easy enough...

Here's the hbest I can do, in two queries:

THE COUNT:
SELECT Count(*) AS Patients, ClinicName
 FROM (SELECT DISTINCT PatientID, Name FROM Data ORDER  BY Name, PatientID)

GROUP BY Name
ORDER BY Name;

THE SUMS:
SELECT Name, Sum(Amount)
FROM Data
GROUP BY Name;

Is there a way to combine these into one query.  I was think maybe a JOIN of some kind.  In the first query, if I add the Sum(Amount) to the sub-select then I loose the distinct count of the PatientIDs.

I'm baffled.
thanks again

Sample Data

Name               PatientID          Amount
Vet Pets             100              350.00
Vet Pets             100              150.00
Bowsers              101               99.00
Bowsers              102              100.00

Desired Results

Clinic             Patients           Amount     
Vet Pets              1               500.00
Bowsers               2               199.00


 


Answer from Richard Rost:

You can EASILY do this with an aggregate query. Turn on TOTALS and then select Group By for clinic name, Count for patient id, and Sum for amount. If you then need the SQL from it, switch over to SQL view and there you go.

To do this, use an Aggregate Query... also called a Summary Query or Totals Query. There is a little button on the query toolbar that looks like a wierd "E" (actually the Greek letter Sigma). Press that and you get a new Total row. Just add the fields you want to group by (like customer name or ID) and the fields you want to run calculations on (like SUM, Count or Average) and there you go.

I have a FREE tutorial on my web site that teaches you how to create an Aggregate Query. See:

http://599cd.com/tips/access/aggregate-query?key=BlFAlEx

Enjoy!

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 6/22/2026 1:00:45 PM. PLT: 1s