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 612893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:56:32+00:00 2026-05-13T17:56:32+00:00

HI, This should be easy but I don’t understand enough about how grouping works.

  • 0

HI,

This should be easy but I don’t understand enough about how grouping works.

Basically I have 2 tables “Categories” and “Items”

Categories
ID
CategoryName

Items
ID
CategoryID
ItemName
Photo
Score

All I want to do is get 1 row for each category which contains the Category ID, the Category Name and the photo that belongs to the highest scoring item.

So I have tried joining the categories to the items and grouping by the CategoryID. Trouble is that I want to order the items so that the highest scoring items are at the top before it does the groupings to make sure that the photo is from the current highest scoring item in that category. If I select MAX(I.score) I can get the highest score but I’m not sure how to get accompanying photo as MAX(photo) will obviously give me the photo with the highest file name alphabetically.

I hope I’ve explained that well.

  • 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-13T17:56:32+00:00Added an answer on May 13, 2026 at 5:56 pm

    You could try something like (Full example)

    DECLARE @Categories TABLE(
        ID INT,
        CategoryName VARCHAR(50)
    )
    
    DECLARE @Items TABLE(
        ID INT,
        CategoryID INT,
        ItemName VARCHAR(50),
        Photo VARCHAR(50),
        Score FLOAT
    )
    
    INSERT INTO @Categories (ID,CategoryName) SELECT 1, 'Cat1'
    INSERT INTO @Categories (ID,CategoryName) SELECT 2, 'Cat2'
    
    INSERT INTO @Items (ID,CategoryID,ItemName,Photo,Score) SELECT 1, 1, 'Item1', 'PItem1', 1
    INSERT INTO @Items (ID,CategoryID,ItemName,Photo,Score) SELECT 2, 1, 'Item2', 'PItem2', 2
    INSERT INTO @Items (ID,CategoryID,ItemName,Photo,Score) SELECT 3, 1, 'Item3', 'PItem3', 3
    
    INSERT INTO @Items (ID,CategoryID,ItemName,Photo,Score) SELECT 4, 2, 'Item4', 'PItem4', 5
    INSERT INTO @Items (ID,CategoryID,ItemName,Photo,Score) SELECT 5, 2, 'Item5', 'PItem5', 2
    
    SELECT  *
    FROM    (
                SELECT  c.ID,
                        c.CategoryName,
                        i.Photo,
                        i.Score,
                        ROW_NUMBER() OVER(PARTITION BY i.CategoryID ORDER BY i.Score DESC) RowID
                FROM    @Categories c INNER JOIN
                        @Items i ON c.ID = i.CategoryID
            ) CatItems
    WHERE   RowID = 1
    

    Using the ROW_NUMBER you can selet the items you require.

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

Sidebar

Related Questions

Ok, this should be really easy, but I just don't have enough experience. I
This should be easy, but I'm not sure how to best go about it.
This seemed like it should be easy, but I have had trouble getting it
Seems like in Perl this should be easy or a module, but I have
This should be easy but I'm having a hard time with it. Basically, I
This should be an easy question hopefully but as a rookie I don't know
This seems like this should be easy but I have not been able to
OK, I'm feeling like this should be easy but am obviously missing something fundamental
This should be an easy problem but... I need to format a currency for
This should be easy for many of you, but for me it's just another

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.