Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / General 1 / September 2004

Tip: Looking for answers? Try searching our database.

SQL count across two tables in a one to many relationship

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Al Findlay - 09 Sep 2004 10:13 GMT
Hello!

I have two tables, USERS and PHOTOS, in a one to many relationship like this...

USERS:
UserID:  Name:
1        Bob
2        Frank
3        Jim
4        Bill
5        Keith

PHOTOS:
PhotoID: ImagePath:   OwningUserID:
A        test.jpg     1
B        test2.jpg    1
C        test3.jpg    2
D        test4.jpg    2
E        test5.jpg    3

So, here we see:
- Bob has 2 photos, test.jpg and test2.jpg
- Frank has 2 photos, test3.jpg and test4.jpg
- Jim has one photo, test5.jpg
- Bill and Keith have no photos

What I need is an SQL query that returns this recordset:

QUERYRESULT:
Name:    NumberOfPhotos:
Bob      2
Frank    2
Jim      1
Bill     0
Keith    0

Can anybody help?

Thanks,

Al
LoopyNZ - 09 Sep 2004 13:21 GMT
Try this, Al, and let me know how it goes:

SELECT USERS.Name, Count(PHOTOS.PhotoID) AS NumberOfPhotos
FROM USERS LEFT JOIN PHOTOS ON USERS.UserID = PHOTOS.OwningUserID
GROUP BY USERS.Name
ORDER BY Count(PHOTOS.PhotoID) DESC;

------------
LoopyNZ
------------
Al Findlay - 10 Sep 2004 09:59 GMT
Hello!

Perfect. Thank you very much.

Al

> Try this, Al, and let me know how it goes:
>
> SELECT USERS.Name, Count(PHOTOS.PhotoID) AS NumberOfPhotos
> FROM USERS LEFT JOIN PHOTOS ON USERS.UserID = PHOTOS.OwningUserID
> GROUP BY USERS.Name
> ORDER BY Count(PHOTOS.PhotoID) DESC;
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.