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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:23:10+00:00 2026-06-04T03:23:10+00:00

I’ve a small database with 4 tables: Customer – Order – OrderLine – Product,

  • 0

I’ve a small database with 4 tables: Customer – Order – OrderLine – Product, I’m playing around a bit with queries and am trying to fetch the names of the people with the most expensive order.

After some playing around I’ve come up with the following query, which shows the prices of all orders:

SELECT SUM(OrderLine.Amount * Product.Price) AS OrderLinePrice, Orders.Id, Customer.Lastname
FROM OrderLine, Product, Orders, Customer
WHERE OrderLine.ProductId = Product.Id
AND Orders.Id = OrderLine.OrderId
AND Customer.Id = Orders.CustomerId
GROUP BY Orders.Id, Customer.Lastname
ORDER BY OrderLinePrice DESC

Now what I conceptually need to do, or think I need to do, is apply a MAX() to select only the highest OrderLinePrice, but I’m not succeeding with SQL Server complaining about not being able to perform aggregate functions on an expression that contains an aggregate…

=============== Update:

Currently my query looks like this:

SELECT t.CustomerLastName
FROM (
    SELECT SUM(OrderLine.Amount * Product.Price) AS OrderLinePrice, Orders.Id, Customer.Lastname AS CustomerLastName
    FROM OrderLine, Product, Orders, Customer
    WHERE OrderLine.ProductId = Product.Id
    AND Orders.Id = OrderLine.OrderId
    AND Customer.Id = Orders.CustomerId
    GROUP BY Orders.Id, Customer.Lastname
    ) AS t
WHERE t.OrderLinePrice = 
(
    SELECT MAX(s.OrderLinePrice) AS MaxOrderPrice
    FROM (
        SELECT SUM(OrderLine.Amount * Product.Price) AS OrderLinePrice, Orders.Id, Customer.Lastname AS CustomerLastName
        FROM OrderLine, Product, Orders, Customer
        WHERE OrderLine.ProductId = Product.Id
        AND Orders.Id = OrderLine.OrderId
        AND Customer.Id = Orders.CustomerId
        GROUP BY Orders.Id, Customer.Lastname
        ) AS s
)
ORDER BY CustomerLastName

This retrieves a list of customers from which the price of their order is equal to the price of the most expensive order. This retrieves exactly what I want, but it feels horribly redundant.

How should I start making this more efficient (if possible)?

  • 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-04T03:23:11+00:00Added an answer on June 4, 2026 at 3:23 am

    First of all, have you considered using explicit joins? It might just be a matter of taste, but perhaps you’ll find queries with explicit joins clearer than those with implicit ones (or “comma” joins), like in your example.

    As for the question, you could use TOP (1) WITH TIES, like this:

    WITH ranked AS (
      SELECT
        SUM(ol.Amount * p.Price) AS OrderLinePrice,
        o.Id,
        c.Lastname AS CustomerLastName
      FROM OrderLine ol
        INNER JOIN Product  p ON p.Id = ol.ProductId
        INNER JOIN Orders   o ON o.Id = ol.OrderId
        INNER JOIN Customer c ON c.Id = o.CustomerId
      GROUP BY Orders.Id, Customer.Lastname
    )
    SELECT *
    FROM (
      SELECT TOP (1) WITH TIES CustomerLastName
      FROM ranked
      ORDER BY OrderLinePrice DESC
    ) s
    ORDER BY CustomerLastName
    

    Or you could use RANK() or DENSE_RANK(), like this:

    WITH ranked AS (
      SELECT
        SUM(ol.Amount * p.Price) AS OrderLinePrice,
        o.Id,
        c.Lastname AS CustomerLastName,
        RANK() OVER (ORDER BY SUM(ol.Amount * p.Price) DESC) AS rnk
      FROM OrderLine ol
        INNER JOIN Product  p ON p.Id = ol.ProductId
        INNER JOIN Orders   o ON o.Id = ol.OrderId
        INNER JOIN Customer c ON c.Id = o.CustomerId
      GROUP BY Orders.Id, Customer.Lastname
    )
    SELECT CustomerLastName
    FROM ranked
    WHERE rnk = 1
    ORDER BY CustomerLastName
    

    If you are only interested in the topmost total price, RANK() is enough, but if you ever want customers for top n totals, use DENSE_RANK() instead and change the condition from rnk = 1 to rnk <= n.

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

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.