Home >Database >Mysql Tutorial >How to Retrieve Transaction Data for the Last 7 Days, Including Days with No Transactions in MySQL?

How to Retrieve Transaction Data for the Last 7 Days, Including Days with No Transactions in MySQL?

Susan Sarandon
Susan SarandonOriginal
2024-12-31 14:38:10875browse

How to Retrieve Transaction Data for the Last 7 Days, Including Days with No Transactions in MySQL?

Selecting Rows from the Last 7 Days, Including Empty Rows in a Single Table

In a MySQL database, you want to retrieve transactions from the past 7 days and calculate the total sales amount for each day. However, you want to ensure that even if no data is available for a particular day, an empty row is included in the result set.

You have attempted to address this issue, but your query only retrieves the days with transactions. To achieve the desired outcome, you need to modify your approach.

Solution:

The solution involves utilizing a subquery to generate a list of dates within the past 7 days. This list is then left joined with the transactions table to create a comprehensive result set.

  1. Generate a List of Dates:
SELECT DATE_FORMAT(a.Date,'%Y-%m-%d') as purchase_date,
'0' as  amount
FROM (
    SELECT curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
    FROM (SELECT 0 as a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) as a
    CROSS JOIN (SELECT 0 as a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) as b
    CROSS JOIN (SELECT 0 as a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) as c
) a
WHERE a.Date BETWEEN NOW() - INTERVAL 7 DAY AND NOW()
  1. Join with Transactions Table:
LEFT JOIN
(
  SELECT DATE_FORMAT(purchase_date, '%Y-%m-%d') as purchase_date,
  coalesce(SUM(amount), 0) AS amount
  FROM transactions
  WHERE purchase_date BETWEEN NOW() - INTERVAL 7 DAY AND NOW()
  AND vendor_id = 0
  GROUP BY purchase_date
)t2
ON t2.purchase_date = t1.purchase_date
  1. Group and Order Results:
GROUP BY t1.purchase_date
ORDER BY t1.purchase_date DESC

This query combines the generated days with the transactions table, ensuring that even for days without transactions, empty rows are included. The final result set will display the purchase dates and total sales amounts for the past 7 days, with empty rows indicating no transactions for those days.

The above is the detailed content of How to Retrieve Transaction Data for the Last 7 Days, Including Days with No Transactions in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn