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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:08:49+00:00 2026-05-24T12:08:49+00:00

Essentially I need to group unique product data into a single row when: The

  • 0

Essentially I need to group unique product data into a single row when:

  • The supplier sku matches
  • The price for the products match (or) 1 of the product lines is equal to ‘0.00’

Here is a sample dataset set along with a working query for what I’m trying to accomplish. I’m simply not entirely comfortable that this is the best way to perform this query.

DECLARE @Test TABLE
(
SupplierSKU VARCHAR(25),
Description VARCHAR(50),
Quantity VARCHAR(25),
Price VARCHAR(25)
)

INSERT INTO @Test
SELECT '123', 'APPLES', '15', '0.00'
INSERT INTO @Test
SELECT '124', 'ORANGES', '10', '15.34'
INSERT INTO @Test
SELECT '123', 'APPLES', '5', '27.40'
INSERT INTO @Test
SELECT '125', 'PLUMS', '67', '34.86'
INSERT INTO @Test
SELECT '124', 'ORANGES', '10', '15.78'
INSERT INTO @Test
SELECT '125', 'PLUMS', '3', '34.86'


SELECT SupplierSKU, Description, SUM(Quantity) AS [Quantity], MAX(Price) AS [Price]
FROM
(
    SELECT SupplierSKU, Description, SUM(CAST(Quantity AS INT)) AS [Quantity], (SELECT MAX(CAST(Price AS MONEY)) AS [Price] FROM @Test ti WHERE ti.SupplierSKU = t.SupplierSKU AND ti.Price = t.price AND ti.Price <> '0.00') AS [Price]
    FROM @Test t
    GROUP BY SupplierSKU, Description, Price
) pdata
GROUP BY pdata.SupplierSKU, pdata.Description

The desired results:

SupplierSKU Description Quantity    Price 
123         APPLES          20      27.40 
124         ORANGES         10      15.34
124         ORANGES         10      15.78  
125         PLUMS           70      34.86
  • 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-24T12:08:49+00:00Added an answer on May 24, 2026 at 12:08 pm

    First I repaired your own solution

    SELECT SupplierSKU, Description, SUM(Quantity) AS [Quantity], MAX(cast(Price as money)) AS [Price] 
    FROM 
    ( 
        SELECT SupplierSKU, Description, SUM(CAST(Quantity AS INT)) AS [Quantity], 
        (SELECT MAX(cast(PRICE as money)) from @test 
        where t.SupplierSKU = SupplierSKU and Description = t.Description and (t.price = '0.00' or t.price = price)) price
        FROM @Test t 
        GROUP BY SupplierSKU, Description , price
    ) pdata 
    GROUP BY pdata.SupplierSKU, pdata.Description , price
    

    Then I rewrote your solution to something more readable

    SELECT suppliersku, description, sum(cast(quantity as int)) quantity, max(cast(price as money)) price FROM (
    SELECT suppliersku, description, quantity, price FROM @test
    WHERE price <> '0.00'
    UNION ALL 
    SELECT t1.suppliersku, t1.description, t1.quantity, max(t2.price)
    FROM @test t1
    join
    @test t2 ON t1.SupplierSKU=t2.SupplierSKU and t1.Description = t2.Description
    WHERE t1.price = '0.00'
    GROUP BY t1.suppliersku, t1.description, t1.quantity
    ) a
    GROUP BY suppliersku, description, price
    

    You may notice i get the right quantity of apples (15+5 = 20)

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

Sidebar

Related Questions

So I essentially need to do this: String text = line1\n; text += line2\n;
Essentially I need a count of each Entries Comments: SELECT e.*, COUNT(c.id) as comments
I need to parse a simple statement (essentially a chain of function calls on
Essentially, I need to parse the response string of the CHAT CREATE command with
I've got a bit of a problem. Essentially, I need to store a large
Essentially, I have to get a flat file into a database. The flat files
We've been discussing design of a data warehouse strategy within our group for meeting
I need to write a query in sql server to data get like this.
I have an asp.net listview. The data that populates it is sorted into groups,
I essentially need to create a version of the following website using JavaScript and

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.