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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:15:51+00:00 2026-06-16T22:15:51+00:00

I have a sale table includes purchases/returns/exchanges Sample: ——–**saleTbl**———— CustID | DOP | SKU

  • 0

I have a sale table includes purchases/returns/exchanges
Sample:

--------**saleTbl**------------
CustID | DOP | SKU | Price
111 | 11/05/12 | 001 | 45.99
222 | 11/20/12 | 001 | 45.99
111 | 11/06/12 | 002 | 40.95
111 | 11/06/12 | 001 | -45.99
111 | 11/19/12 | 004 | 50.00
222 | 11/25/12 | 003 | 20.99
111 | 12/01/12 | 002 | -40.95
111 | 12/01/12 | 003 | 20.99

Criteria is: find total for each customer during 11/05/12 – 11/20/12. If customer exchanged the item that was purchased during that time and purchase with the same day will be count.
The expected result is:

CustID | DOP | Price
222 | 11/20/12 | 45.99
111 | 12/01/12 | 70.99

I have tried to get the total but of course it is not right:

SELECT DISTINCT [num_cp] AS 'Member Id'
      ,MAX([dop]) AS 'Date'
      ,SUM([price]) AS 'Point'
  FROM [Mailing_List].[dbo].[UGG_DoublePoint]
  WHERE [num_cp] IN
  (
    SELECT [num_cp]
    FROM [Mailing_List].[dbo].[UGG_DoublePoint]
    GROUP BY [num_cp]
    HAVING SUM([price]) >0
  )
  --AND 
  AND [dop] BETWEEN '11/05/12' AND '11/20/12'
  GROUP BY [num_cp]

Please help! Thanks everyone.

  • 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-16T22:15:52+00:00Added an answer on June 16, 2026 at 10:15 pm

    I think you need to change your query to this

    ; WITH CTE AS 
    (
        SELECT *, 
        COUNT(*) OVER (PARTITION BY CustID, DOP) Row_Cnt
        FROM TEST
    ), CTE2 AS 
    (
        SELECT * FROM CTE
        WHERE [dop] BETWEEN '11/05/12' AND '11/20/12'
    ), CTE3 AS 
    (
        SELECT * FROM CTE2 WHERE price > 0
        UNION
        SELECT * FROM CTE2 WHERE price < 0 
        and SKU IN (SELECT SKU FROM CTE2 WHERE Price > 0)
        UNION
        SELECT * FROM CTE
        WHERE row_cnt > 1 and DOP IN (
        SELECT max(A.dop) d FROM CTE A
        INNER JOIN CTE2 B ON A.CustID = B.CustID AND A.SKU = B.SKU
        )
     )
    SELECT Custid, max(dop) dateid, sum(price) Price
    from cte3
    group by custid;
    

    Check SQL Fiddle Demo

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

Sidebar

Related Questions

I have a table sort of like this: Product Price Sale ------------------------ Watch 10.00
I have a query that returns Properties for Rent / Sale on their respective
I have a table that maintains rows of products that are for sale (
I need to create a fifo function for price calculation. I have a table
I have a order table and I want to get the total sale till
I have a sales table that shows the date and time of each sale.
I have stock table like below: date item new stock sale total 1-1-2000 abc
Say I have these three tables: Table: Baskets id | name 1 Sale 2
I have a secondhand sale car database and four tables with relationships. Same column
Suppose you have to following tables where a sale consists of products and a

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.