LeetCode-1321. Restaurant turnover growth change (moderate)

表: Customer

+ --------- + --------------- +
| Column the Name | Type |
+ --------------- + + ---------
| CUSTOMER_ID | int |
| name | VARCHAR |
| visited_on | DATE |
| AMOUNT | int |
+ --------------- + --- + ------
(CUSTOMER_ID, visited_on) is the primary key for the table
of the customer transaction data table contains a restaurant
customer visited_on representation (customer_id) in the day visited the restaurant visited_on
amount is the total spending one day a customer of
 

You are the restaurant owner, and now you want to analyze possible changes in revenue growth (at least one customer a day)

Write a SQL query to calculate the average customer spending seven days (a + 6 days before the date of that date) is a period of time

Examples of query results format is as follows:

Sort query results by visited_on
average_amount to two decimal places, the format of date data for ( 'YYYY-MM-DD' )
 

Customer 表:
+-------------+--------------+--------------+-------------+
| customer_id | name         | visited_on   | amount      |
+-------------+--------------+--------------+-------------+
| 1           | Jhon         | 2019-01-01   | 100         |
| 2           | Daniel       | 2019-01-02   | 110         |
| 3           | Jade         | 2019-01-03   | 120         |
| 4           | Khaled       | 2019-01-04   | 130         |
| 5           | Winston      | 2019-01-05   | 110         | 
| 6           | Elvis        | 2019-01-06   | 140         | 
| 7           | Anna         | 2019-01-07   | 150         |
| 8           | Maria        | 2019-01-08   | 80          |
| 9 | jaze | 01/09/2019 | 110 | 
| 1 | Jhon | 10/01/2019 | 130 | 
| 3 | jade | 10/01/2019 | 150 | 
+ -------------- + ------------- + -------------- + ----- -------- +

Results Table:
+ ---------------- + -------------- -------------- + +
| visited_on | AMOUNT | average_amount |
+ ----------- + -------------- -------------- + + -----
| 2019-01-07 | 860 | 122.86 |
| 2019-01-08 | 840 | 120 |
| 2019-01-09 | 840 | 120 |
| 2019-01-10 | 1000 | 142.86 |
+ -------------- + ---------------- + -------------- +

The first seven days the average consumer from 2019-01-01 to 2019-01-07 (100 + 110 + 120 + 130 + 110 + 140 + 150) / 7 = 122.86
second seven days the average consumer from 2019-01 2019-01-08 -02 to (110 + 120 + 130 + 110 + 140 + 150 + 80) / 120 = 7
third seven days the average consumption is from 2019-01-03 to 2019-01-09 (120 + 130 + 110 + 140 + 150 + 80 + 110) / 120 = 7
fourth seven days from 2019-01-04 to 2019-01-10 average consumption is (130 + 110 + 140 + 150 + 80 + 110 + 130 + 150) / 7 = 142.86

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/restaurant-growth
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Moderation: Write a SQL query to calculate the average customer spending seven days (a + 6 days before the date of that date) is a period of time

Thoughts: query the current average of six days before this date.

Problem solving:

Method One: first select the range, and then statistics

SELECT t1.visited_on, sum(t2.amount) amount, ROUND(SUM(t2.amount)/7.0, 2) average_amount
FROM
(SELECT DISTINCT visited_on FROM customer
WHERE visited_on >= DATE_ADD((SELECT min(visited_on) FROM customer), INTERVAL 6 DAY)) t1
LEFT JOIN
customer t2
ON t1.visited_on <= DATE_ADD(t2.visited_on, INTERVAL 6 DAY) AND t1.visited_on >= t2.visited_on
GROUP BY t1.visited_on;

Method Two:

First to go heavy sum for each table by date, and then do the Cartesian product, then screened to meet the conditions, and then after ordering. Get answers

select c1.visited_on,sum(c2.amount) amount,round(sum(c2.amount)/7,2) average_amount
from 
(select customer_id,name,visited_on,sum(amount) amount from Customer group by visited_on)c1 cross join 
(select customer_id,name,visited_on,sum(amount) amount from Customer group by visited_on) c2 
where datediff(c1.visited_on,c2.visited_on)<7  and datediff(c1.visited_on,c2.visited_on)>=0
group by c1.visited_on
having count(*)>6
order by c1.visited_on

 Knowledge points:

DATE_ADD () function to add to the date specified time interval.

DATE_ADD (DATE, INTERVAL expr of the type)
 DATE  parameter is valid date expression. expr  parameter is the time interval you want to add.
Published 144 original articles · won praise 2 · Views 5733

Guess you like

Origin blog.csdn.net/Hello_JavaScript/article/details/104783384