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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:50:29+00:00 2026-06-01T12:50:29+00:00

The query I’m trying to develop return the following: The total amount of units

  • 0

The query I’m trying to develop return the following:

  1. The total amount of units sold of each product
  2. The name of the product
  3. The CustomerID that has bought the max amount of each product

This is what I have so far:

SELECT DISTINCT 
   Products.ProductName, 
   SUM([Order Details].Quantity) as cant, 
   Orders.CustomerID       
FROM 
  Products 
INNER JOIN [Order Details] 
    ON Products.ProductID = [Order Details].ProductID 
INNER JOIN Orders 
    ON [Order Details].OrderID = Orders.OrderID
WHERE 
  [Order Details].Quantity = 
  (
      SELECT 
        MAX([Order Details].Quantity) 
      FROM 
        [Order Details]
      WHERE 
        [Order Details].ProductID = Products.ProductID
  )
GROUP BY 
  Products.ProductName, Orders.CustomerID

It’s not giving me the results expected.

Any information related to the contents of the tables or anything else, just post it in a comment and I’ll answer.

Thanks in advance for the help!

  • 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-01T12:50:30+00:00Added an answer on June 1, 2026 at 12:50 pm

    Try this,

    ;WITH orderCTE AS 
    (   SELECT  p.ProductName,
                o.CustomerID,
                SUM(od.Quantity) [Quantity]
        FROM    Products p
                INNER JOIN [Order Details] od
                    ON od.ProductID = p.ProductID
                INNER JOIN Orders o
                    ON o.OrderID = od.OrderID
        GROUP BY p.ProductName, o.CustomerID
    )
    SELECT  ProductName, [TotalQuantity], CustomerID
    FROM    (   SELECT  CustomerID, 
                        ProductName,
                        Quantity,
                        MAX(Quantity) OVER(PARTITION BY ProductName) [MaxQuantity],
                        SUM(Quantity) OVER(PARTITION BY ProductName) [TotalQuantity]
                FROM    orderCTE
            ) ord
    WHERE   MaxQuantity = Quantity  
    

    EDIT

    The above will return duplicates if more than one customer has order the same product the maximum amount of times. This can be avoided by using the below, which will return a semi-colon separated list of customer IDs that have order each product the max number of times:

    ;WITH orderCTE AS 
    (   SELECT  p.ProductName,
                o.CustomerID,
                SUM(od.Quantity) [Quantity]
        FROM    Products p
                INNER JOIN [Order Details] od
                    ON od.ProductID = p.ProductID
                INNER JOIN Orders o
                    ON o.OrderID = od.OrderID
        GROUP BY p.ProductName, o.CustomerID
    ), MaxOrdersCTE AS
    (   SELECT  CustomerID, 
                ProductName,
                Quantity,
                MAX(Quantity) OVER(PARTITION BY ProductName) [MaxQuantity],
                SUM(Quantity) OVER(PARTITION BY ProductName) [TotalQuantity]
        FROM    orderCTE
    )
    
    SELECT  ProductName, 
            [TotalQuantity], 
            STUFF(( SELECT  ';' + CONVERT(VARCHAR, CustomerID)
                FROM    MaxOrdersCTE c
                WHERE   ord.ProductName = c.Productname
                AND     MaxQuantity = Quantity
                FOR XML PATH('')
            ), 1, 1, '') [CustomerIDs]
    FROM    MaxOrdersCTE ord
    WHERE   MaxQuantity = Quantity 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following query is used for Getting Categories and one news for each category. How
Query: SELECT product_id, name FROM product WHERE barcode in (681027, 8901030349379, 679046, 679047, 679082,
Query: Given a cuisineID (type of cuisine), return the list of restaurants that offer
Django query gives me below output format,but i want below format data=`[{'total': 1744, 'name:
Query always return -1 don't know why. Will someone please explain. Value of count
Query to find All bars that sell three different beers at the same price?
Query : select i.Name,ri.Country,ri.State,ri.City from Information as i join ResidenceInformation as ri order by
Query to get total commissions for an employee, and update their totalCommission column in
query = Files .Where(file => file.Fileinfo.Name.ToUpper().Contains(textBox1.Text.ToUpper())) .Take(7).ToList(); I hate asking this question, but I
Query I'm using: SELECT COUNT(*), SUM(amount) AS amount, FROM_UNIXTIME(added, '%W (%e/%m)') AS daily FROM

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.