Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6198129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:56:35+00:00 2026-05-24T03:56:35+00:00

I’m having a little trouble making an SQL SERVER 2000 query. Here is my

  • 0

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,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:56:35+00:00Added an answer on May 24, 2026 at 3:56 am

    Basically something like this:

    SELECT
      f.pk_folderID,
      f.folderName,
      VideoCount = COUNT(CASE m.MediaType WHEN 'Video' THEN 1 END),
      ImageCount = COUNT(CASE m.MediaType WHEN 'Image' THEN 1 END)
    FROM Folder f
      LEFT JOIN FolderMedia fm ON f.pk_folderID = fm.fk_folderID
      LEFT JOIN Media m ON fm.fk_media = m.pk_media
    WHERE f.fk_userID = @UserID
    GROUP BY
      f.pk_folderID,
      f.folderName
    

    UPDATE (based on the additional request):

    To include a sort of TOP 1 Media.Name into the result set, the above query could be changed like this:

    SELECT
      f.pk_folderID,
      f.folderName,
      VideoCount = COUNT(CASE m.MediaType WHEN 'Video' THEN 1 END),
      ImageCount = COUNT(CASE m.MediaType WHEN 'Image' THEN 1 END),
      MediaName  = MAX(CASE fm.timestamp WHEN t.timestamp THEN m.Name END)
    FROM Folder f
      LEFT JOIN FolderMedia fm ON f.pk_folderID = fm.fk_folderID
      LEFT JOIN Media m ON fm.fk_media = m.pk_media
      LEFT JOIN (
        SELECT
          fk_folderID,
          timestamp = MIN(timestamp)
        FROM FolderMedia
        GROUP BY fk_folderID
      ) t ON fm.fk_folderID = t.fk_folderID AND fm.timestamp = t.timestamp
    WHERE f.fk_userID = @UserID
    GROUP BY
      f.pk_folderID,
      f.folderName
    

    In cases where minimal FolderMedia.timestamp values are not unique within their folders, the ultimate value of the corresponding Media.Name will be decided by its alphabetical sorting. In particular, the above query selects the last one of the set (with MAX()).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.