问题
i have 1 table data buyer called order_match which contain coloumn createdby as the buyer, createdAt as the date of transaction, and order_status_Id as the order_status, if order_status_id (4,5,6,8) then the transaction are approved, so i want to count every differiante days of each buyer so i know the gap of each buyer to doing new transaction after the last transaction, so after that i can count the max day of the longest transaction gap, and the minimum day, and the Average of the buyer on that range to doing transaction
this is the example data of the transaction between 2018-12-01 until 2018-12-04
+-----------+------------+-----------------+
| createdby | createdAt | order_status_id |
+-----------+------------+-----------------+
| A | 2018-12-01 | 4 |
| A | 2018-12-02 | 5 |
| A | 2018-12-04 | 5 |
| B | 2018-12-02 | 5 |
| B | 2018-12-04 | 5 |
| C | 2018-12-03 | 5 |
| C | 2018-12-04 | 6 |
+-----------+------------+-----------------+
from that data, what expected results is :
+-------------------------------+---------+---------------------------------------+
| max day | min day | average day of the transaction |
+-------------------------------+---------+---------------------------------------+
| 2 (the transaction on | 1 | 1,5 (average from each gap |
| buyer "A" ('2018-12-02' to | | day / of the gap transaction held) |
| '2018-12-04) and "B" | | |
| ('2018-12-02' to '2018-12-04) | | |
+-------------------------------+---------+---------------------------------------+
the point is i want to count time lag in days between every two consecutive purchases made by the buyer within the selected time frame. the difference in days also calculates the time lag between the last purchase before the selected time range (if any) and the first purchase in the selected time range.
this is my progress syntax
SELECT sum((DATEDIFF(greatest(om1.createdAt, '2018-12-18'), least(om2.createdAt, '2018-12-20'))*-1)+1)
FROM order_match om1, order_match om2
WHERE om1.createdAt <='2018-12-20' AND om2.createdAt >= '2018-12-18' and
om1.order_status_Id in (4, 5, 6, 8) group by om1.createdby;
来源:https://stackoverflow.com/questions/60350526/count-the-differiante-date-of-each-users-mysql