Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Query of distinct values

Posted on 2009-05-07
Medium Priority
Last Modified: 2013-11-27
I have a table that has the following values:
ID     Person      Units
128   Greg        8
129   Greg        10
130   Greg         5
131   Greg         6
131   Greg         4
132   Sally         10
133   Sally         5
133   Sally         8
133   Sally         15

I need to write a query that will sum up by person the number of unique Id's and the total number of units.
In this case, it should show:
     Greg     4 (id's)   33 (units)
     Sally     2 (id's)   38 (units)

The kicker is that the id's are not unique, so I need a count of the unique records for each person , but the sum of the units must contain all of the units.


Question by:twands
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 25

Expert Comment

ID: 24332753
Hi twands,


SELECT Person, count(distinct ID) as idcnt, SUM(Units)
FROM yourtable

LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24332758
select Person, sum(units) as totl
from urTable
group by Person

Author Comment

ID: 24332813

I have tried your solution, but to my knowledge "count(distinct ID) as idcnt" is not supported in access.  It returns an error.
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.


Author Comment

ID: 24332818
Your solution gives me the person and number of units but not the count of unique record id's.
LVL 25

Accepted Solution

lwadwell earned 1000 total points
ID: 24332838

OK ... that is a shame ... try this (not very nice I must admit)

SELECT t1.Person, t2.idcnt, SUM(t1.Units)
FROM yourtable t1
INNER JOIN (SELECT v1.Person, COUNT(*) as idcnt FROM (SELECT DISTINCT Person, ID FROM yourtable) v1 GROUP BY v1.Person) t2
ON t1.Person = t2.Person
GROUP BY t1.Person, t2.idcnt


Author Closing Comment

ID: 31579298
Thanks much!  With a few minor tweaks, I was able to incorpoarte your solution into my query and it gives me what I was looking for.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question