I write customer loyalty software for the club, which opens from 10 AM to 6 AM every day. The data is stored in MYSQL, and I would like to calculate the total number of customer visits per month.
I use an account (excellent (date)), but if the player arrived at 5 pm and stayed until 3 in the morning with 2 transactions at 22:00 and 2:00. It will be counted as 2 visits instead of 1.
I have a transaction table with the columns listed below:
ps: everything in brackets () is not real data. I get about 2000 transactions per day. I can also change the structure of the table
Transaction_ID | Date (not Date / Time) | Customer_ID | Item | price | timestamp
1 | 11-06-2015 (6pm) | Jane | drink | 2.00 | 156165166
2 | 06-06-2015 (2pm) | Jane | drink | 2.00 | 1433858493
3 | 06-06-2015 (3am) | Jane | drink | 2.00 | 1433906073
4 | 06-06-2015 (6pm) | Jane | drink | 2.00 | 156165166
The current code returns {4, Jane}. The answer I'm looking for is {3, Jane}. Transaction {2,3} should be considered as one visit
SELECT count(distinct(Date)) as visit, Customer_ID FROM transaction GROUP BY Customer_ID WHERE timestamp BETWEEN $timestamp1 AND $timestamp2
$ timestamp1 = strtotime ("first day of february + 10am");
$ timestamp2 = strtotime ("first day of march + 6am"); How do you suggest accurately calculating the total number of visits below? I can change the structure of the table from date to date / time.
The easiest answer with minimal changes to my codes.
SELECT count (DISTINCT (DATE (DATE_SUB (from_unixtime (timestamp), INTERVAL 6 HOUR))) as visit, Customer_ID
FROM transaction
GROUP BY Customer_ID
WHERE timestamp BETWEEN $ timestamp1 AND $ timestamp2
database php mysql
Boon yao tan
source share