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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:47:19+00:00 2026-05-27T11:47:19+00:00

How can i sum values in Result 1 like Result 2 Result 1 SalesRep

  • 0

How can i sum values in Result 1 like Result 2

Result 1
    SalesRep    Customer    Product Quantity    Total
    Doe, John   AA Corp     P1      50          5000
    Doe, John   CA Corp     P2      67          6030
    Doe, John   EA Corp     P3      46          5980
    Doe, John   GA Corp     P2      22          1980
    Doe, John   HA Corp     P3      43          5590
    Doe, John   IA Corp     P1      35          3500 | Sum this two 
    Doe, John   IA Corp     P2      24          2160 | make second record 0
    Doe, John   JA Corp     P2      66          5940
    Doe, John   MA Corp     P4      59          7670 
    Doe, John   OA Corp     P2      43          3870 | Sum this two 
    Doe, John   OA Corp     P4      14          1820 | make second record 0
    Doe, John   PA Corp     P1      89          8900


Result 2
    SalesRep    Customer    Product Quantity    TotalPrice
    Doe, John   AA Corp     P1      50          5000
    Doe, John   CA Corp     P2      67          6030
    Doe, John   EA Corp     P3      46          5980
    Doe, John   GA Corp     P2      22          1980
    Doe, John   HA Corp     P3      43          5590
    Doe, John   IA Corp     P1      59          5660
    Doe, John   IA Corp     P2      0           0
    Doe, John   JA Corp     P2      66          5940
    Doe, John   MA Corp     P4      59          7670
    Doe, John   OA Corp     P2      57          5690
    Doe, John   OA Corp     P4      0           0
    Doe, John   PA Corp     P1      89          8900
  • 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-27T11:47:20+00:00Added an answer on May 27, 2026 at 11:47 am

    One possibility is this, although I really don’t see the business case. I guess it’s just a technical question:

    SELECT 
      T.SalesRep, T.Customer, T.Product,
      CASE WHEN EXISTS (SELECT 1 FROM MyTable AS T1
                        WHERE T1.SalesRep = T.SalesRep
                        AND T1.Customer = T.Customer
                        AND T1.Product < T.Product)
           THEN 0
           ELSE SUM(T.Quantity) OVER (PARTITION BY T.SalesRep, T.Customer)
      END AS Quantity,
      CASE WHEN EXISTS (SELECT 1 FROM MyTable AS T1
                        WHERE T1.SalesRep = T.SalesRep
                        AND T1.Customer = T.Customer
                        AND T1.Product < T.Product)
           THEN 0
           ELSE SUM(T.Total) OVER (PARTITION BY T.SalesRep, T.Customer)
      END AS Total
    FROM MyTable AS T
    

    For the two CASE clauses, they read: “when there is a record for the same sales representative and customer with a lesser product (that’s my assumption?), then make the value zero. Otherwise, sum up all values grouped by sales representative and customer. This will work also with more than two (distinct!) products per sales rep. and customer.

    Note: If there can be several identical products per sales rep. and customer, this won’t work, and you’d have to compare some other values (e.g. T1.ID < T.ID) in the two nested selects

    Note also: this could be wrong, as I had to make some assumptions regarding your requirements.

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

Sidebar

Related Questions

Can you pass variables into subqueries, like so? SELECT users.id AS id, (SELECT SUM(total)
How can I sum all of the values in an array of arrays? Sample
Why can't we use return keyword inside ternary operators in C, like this: sum
Given a table like this: [Last Name] [First Name] [DepartmentID] --------------------------------------- Doe John 1
i am trying to find the max values of a sum result but i
For a map like std::map, how do I accumulate it's values' sum? Actually, I
How can I sum an interval of elements on an array in python /
Can i apply SUM() within an ISNULL() .... Consider my following sql server select
Given an array. How can we find sum of elements in index interval (i,
How can the following simple implementation of sum be faster? private long sum( int

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.