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

  • SEARCH
  • Home
  • 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 8179205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:57:03+00:00 2026-06-06T23:57:03+00:00

Using SQL Server 2005 / 2008 I have a table BB_MEDIAOBJECT (Id, …) ,

  • 0

Using SQL Server 2005 / 2008 I have a table BB_MEDIAOBJECT (Id, ...), which includes several other data-fields for mediaobjects, a table BB_COLLECTION (Id, Name, Description) in which approx 30 collections are defined, and a table BB_MEDIAOBJECT_COLLECTION (MediaObjectId, CollectionId), Which lists all items in each collection.

Please help me with defining a SQL query that gives me the first 10 mediaobjects for each collection.

Based upon the answer supplied by Marc, I’ve ended up with the following end result, in which the query now correctly displays the 10 most recently modified items:

;WITH PartitionedComponents AS  
(  
    SELECT  
        CollectionID = c.ID,  
        c.Name,  
        c.Description, 
        MediaObjectId = m.ID, 
        ROW_NUMBER() OVER(PARTITION BY c.ID ORDER BY m.modifiedby DESC) AS 'RowNum' 
    FROM  
        dbo.BEELDBANK_COLLECTION c 
    INNER JOIN 
        dbo.BEELDBANK_MEDIAOBJECT_COLLECTION mc ON mc.CollectionId = c.ID 
    INNER JOIN 
        dbo.BEELDBANK_MEDIAOBJECT m ON mc.MediaObjectId = m.Id 
) 
SELECT  
    CollectionID,  
    Name,  
    Description, 
    MediaObjectId 
FROM  
    PartitionedComponents 
WHERE 
    RowNum <= 10
  • 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-06-06T23:57:04+00:00Added an answer on June 6, 2026 at 11:57 pm

    One approach would be to use a CTE (Common Table Expression). You can partition your data by some criteria – i.e. your Id for the collection – and have SQL Server number all your rows starting at 1 for each of those partitions, ordered by some other criteria – i.e. probably Id for your media object (you didn’t specify exactly – you want the 10 first objects – but you didn’t say what these would be ordered by; first ten always implies there must be some order – change my CTE query as needed for your case!)

    So try something like this:

    ;WITH PartitionedComponents AS
    (
       SELECT 
           CollectionID = c.ID, 
           c.Name, 
           c.Description,
           MediaObjectId = m.ID,
           ROW_NUMBER() OVER(PARTITION BY c.ID ORDER BY m.ID DESC) AS 'RowNum'
       FROM 
           dbo.BB_Collection c
       INNER JOIN
           dbo.BB_MEDIAOBJECT_COLLECTION mc ON mc.CollectionId = c.ID
       INNER JOIN
           dbo.BB_MEDIAOBJECT m ON mc.MediaObjectId = m.Id
       WHERE
           ......
    )
    SELECT 
       CollectionID, 
       Name, 
       Description,
       MediaObjectId
    FROM 
       PartitionedComponents
    WHERE
       RowNum <= 10
    

    Here, I am selecting the first ten entries for each “partition” (i.e. for each Id) – ordered in a descending fashion by the Id (of MediaObjects).

    Does that approach what you’re looking for??

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

Sidebar

Related Questions

I'm using sql server 2005/2008 and I have a table that it's PK (rec_index)
Using SQL Server 2005 and 2008. I've got a potentially very large table (potentially
[using SQL Server 2005] I have a table full of users, I want to
I am using SQL Server 2005 Express and Visual Studio 2008. I have a
Environment: SQL Server 2005/2008, pubs database I have inserted into a table variable a
DB: SQL Server 2005 We have a table that has data in this manner:
I have a table in SQL Server 2005 which has three columns: id (int),
I am using SQL SERVER 2008, I have a number of INT, SMALLINT fields
I'm using SQL Server 2005 and asp.net 2008 with c#...I have 2 tables Result
I'm using SQL Server 2008 Management studio viewing a 2005 server and have just

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.