How do I count equal values as one?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have three tables. contact, place, firm. contact has PK idContact and FK
idPlace and idFirm. I want to make a query to find number of firms having
contact with each place. In contakt there are several tuples wich shows that
the same firm has several contacts with the same place.
table contact
idContact idPlace idFirm
1 5 4
2 5 4
3 6 5
4 7 9
When I select I want the result to be
Place Number of firms having contact
5 1
6 1
7 1

idFirm is PK in table firm
idPlace is PK in table place
Of course I will display name of place in stead of idPlace shown here.
The problem is how to count idFirm= 4 as 1 instance at idPlace 5
Anyone who has a solution for this query?
 
Something like this, perhaps:

Create a query (call it qryGrouping):
SELECT idPlace AS P, idFirm AS I
FROM TableName
GROUP BY idPlace, idFirm;

Then use this query to get your results:
SELECT TableName.idPlace AS Place,
DCount("I", "qryGrouping", "P=" & TableName.idFirm)
AS FirmNumber
FROM TableName
GROUP BY TableName.idPlace;
 
Tried it out, but Access didn't recognize DCount.
Could you please write the code with the tablenames, because I'm not sure if
you mean TableName = the same tablename in both queries.
I'm new to Access. Therefore I need to know if you mean that the first query
have to be a view. Is it possible to reference a query in an other query
without creating a view?
 
TableName is the generic name I used for the one, single table from which
the data are coming. It is meant to be the same in both queries.

DCount is a function in ACCESS VBA that can be called from a query. If
you're getting an error with that, then you may have a problem with your
references in the ACCESS file. Try following these steps (from a post by
Douglas Steele, MVP):

This can be caused by differences in either the location or file version of
certain files between the machine where the application was developed, and
where it's being run (or the file missing completely from the target
machine). Such differences are common when new software is installed.

On the machine(s) where it's not working, open any code module (or open the
Debug Window, using Ctrl-G, provided you haven't selected the "keep debug
window on top" option). Select Tools | References from the menu bar. Examine
all of the selected references.

If any of the selected references have "MISSING:" in front of them, unselect
them, and back out of the dialog. If you really need the reference(s) you
just unselected (you can tell by doing a Compile All Modules), go back in
and reselect them.

If none have "MISSING:", select an additional reference at random, back out
of the dialog, then go back in and unselect the reference you just added. If
that doesn't solve the problem, try to unselect as many of the selected
references as you can (Access may not let you unselect them all), back out
of the dialog, then go back in and reselect the references you just
unselected. (NOTE: write down what the references are before you delete
them, because they'll be in a different order when you go back in)

For far more than you could ever want to know about this problem, check out
http://www.accessmvp.com/djsteele/AccessReferenceErrors.html

--

Ken Snell
<MS ACCESS MVP>
 
Back
Top