I’m having a little trouble making an SQL SERVER 2000 query. Here is my scenario:
-
I have a table called Folders with 3 columns: pk_folderID, folderName and fk_userID.
-
Also, I have another table called FolderMedia which stores what media (whatever) belong to a certain folder. There are 2 columns: fk_folderID, fk_media.
-
And the last, I have a table called Media which stores some media details. It has a primary key pk_media and among other columns, it has a MediaType column which tells the type of that media: image or video.
Now, I would like a query that does the following:
Select all folders that belong to a certain fk_userID, and then also get the number of media in that folder. I’ve seen a query like this here on StackOverflow, but I didn’t manage to upgrade it to get 2 counts of media (based on their type)
Basically, get the folder details (name, etc) for all folders that belong to a user(fk_userID) and also, for each folder get the number of images and videos in it (as separate values).
The select would basically return:
folderName, count(images in folder), count(videos in folder), other folder details.
One obvious solution would be to just get all folders and then manually calculate the number of images/videos in them… but I would first like to try with a query.
Thank you,
Basically something like this:
UPDATE (based on the additional request):
To include a sort of
TOP 1 Media.Nameinto the result set, the above query could be changed like this:In cases where minimal
FolderMedia.timestampvalues are not unique within their folders, the ultimate value of the correspondingMedia.Namewill be decided by its alphabetical sorting. In particular, the above query selects the last one of the set (withMAX()).