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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:36:03+00:00 2026-05-11T07:36:03+00:00

Short: From below sql select I get the cart_id and the value of the

  • 0

Short:

From below sql select I get the cart_id and the value of the maximum valued item in that cart.

SELECT CartItems.cart_id, MAX(ItemValues.value) FROM CartItems  INNER JOIN ItemValues  ON CartItems.item_id=ItemValues.item_id GROUP BY CartItems.cart_id 

but I also need item_id for that item (ItemValues.item-id).

Long:

Two tables, CartItems, ItemValues (and their respective Carts, Items, irrelevant here).
Each cart can have several items wheras each item has one value defined in ItemValues.
Each item belongs to one cart.
The value of a cart is the value of the item with maximum value within its cart.
How do I select cart-id, max(item-value) and it’s corresponding item-id?

For instance cart-id A contains item-id X with value 10 and item-id Y with value 90.
With above sql select I get,

A, 90

What I need is

 A, Y, 90 

platform: MS SQL

  • 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. 2026-05-11T07:36:04+00:00Added an answer on May 11, 2026 at 7:36 am

    In MS SQL and Oracle:

    SELECT * FROM   (   SELECT ci.*, iv.*,          ROW_NUMBER() OVER (PARTITION BY CartItems.cart_id ORDER BY ItemValues.value DESC)   FROM   CartItems ci   INNER JOIN ItemValues iv      ON CartItems.item_id=ItemValues.item_id   ) s WHERE rn = 1 

    In MySQL:

    SELECT FROM   (   SELECT ci.*,          (          SELECT id          FROM ItemValues iv          WHERE iv.item_id = ci.item_id          ORDER BY                value DESC          LIMIT 1          ) AS maxitem   FROM   CartItems ci   ) iv, ItemValues ivo WHERE ivo.id = iv.maxitem 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get get the short filename from a long filename but I'm
I'm building a website in django that needs to extract key words from short
I need a short code snippet to get a directory listing from an HTTP
I am trying to optimize one SQL that having below explain plan. I am
The SQL below is returning 0 records when I know there are records that
I have a bunch of short videos from my digital cam. i know how
Short: how does modelbinding pass objects from view to controller? Long: First, based on
Short version: How can I map two columns from table A and B if
In short: I want to monitor selected calls from an application to a DLL.
So in short my case is this: Read data from RSS feed Print content

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.