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, 90What I need is
A, Y, 90
platform: MS SQL