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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:12:52+00:00 2026-05-26T05:12:52+00:00

I have a transactions MySQL table on which I record all sales and refunds

  • 0

I have a “transactions” MySQL table on which I record all sales and refunds for my products. Each record thus either represents a sale or a refund (indicated by a flag), the transaction id, the transaction date and the value of the transaction. In case of refunds there is also a refund_id which indicates for which transaction id this refund is for.

Can I, using one query only, calculate how many sales were made in a certain time period (a sale is considered if the refund value is less than the sale value) and the overall amount earned (ie sales value minus refund value).

Notes:
1. There can be at most one refund for every sale.
2. In case of refunds the date to keep in mind is not the date when the refund was done (ie what is recorded in the DB for a particula refund), but when the original sale was done

  • 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-26T05:12:53+00:00Added an answer on May 26, 2026 at 5:12 am
    SELECT SUM(numberofsales) as numberofsales,SUM(salevalue)
    FROM (
    --handle sales with refunds
    SELECT count(sales) as numberofsales, sum(sales.value-refunds.value) as salevalue
    FROM Transactions sales
    LEFT JOIN transactions refunds ON sales.transid=refunds.refundid
    WHERE refunds.refundid IS NOT NULL
    AND sales.value-refund.value>0
    AND sales.date>@begindate
    AND sales.date<@enddate
    AND sales.refund_flag='s'
    UNION
    --handle sales without refunds
    SELECT count(sales) as numberofsales, sum(sales.value) as salevalue
    FROM Transactions sales
    LEFT JOIN transactions refunds ON sales.transid=refunds.refundid
    WHERE refunds.refundid IS NULL
    AND sales.date>@begindate
    AND sales.date<@enddate
    AND sales.refund_flag='s')
    

    This is a first pass with the major charachteristics of the solution.
    It’s using SQL Server syntax, I’m not sure of the differences between sql server and mysql.

    As per michael667’s answer above, you can see the self join to get the refunds for each sale.
    I dont think the group by is required.

    This solution does not attempt to ensure there is only one refund for each sale, and indeed if there is more than one, it will affect the corrrectness of this solution.

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

Sidebar

Related Questions

I have a table Transactions(store_id, item_id, price). I want to find store_id's which sell
I have a table of transactions which will occasionally have duplicate entries. If/When an
I have a MySQL table which basically serves as a file index. The primary
I have like 10 table inserts and each insert has 1 either 1000 or
We have a (currently InnoDB) table which contains roughly 500,000 rows. This represents a
I have a set of transactions occurring at specific points in time: CREATE TABLE
I have an existing database with the table Transactions in it. I have added
I have a MySQL table implementing a mail queue, and I use it also
I have a MySQL database with the following table structure: TransactionType : Transaction_Amount, Transaction_ID,
I have a question about MySQL InnoDB. For example: I have the following table

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.