Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL count distinct visits of a customer with opening hours from 10am to 6am

I am writing a customer loyalty software for a club that opens from 10am to 6am everyday. The data is store in MYSQL and I'd like to count the customer's total visits for the month.

I am using count(distinct(date)) but if the player came at 5pm and stayed till 3am with 2 transactions at 10pm and 2am. It will be counted as 2 visits instead of 1.

I have a transaction table with the columns listed below:

ps: anything in the brackets () is not real data. I get about 2000 transactions a day. I am also able to change the table structure

 Transaction_ID | Date(not Date/Time) | Customer_ID | Item | price | timestamp
   1            | 11-06-2015    (6pm) | Jane        | drink| 2.00  | 156165166
   2            | 09-06-2015    (2pm) | Jane        | drink| 2.00  | 1433858493
   3            | 10-06-2015    (3am) | Jane        | drink| 2.00  | 1433906073
   4            | 06-06-2015    (6pm) | Jane        | drink| 2.00  | 156165166

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 to accurately count the total visits below? I am able to change the table structure from Date to Date/time.

The easiest answer with least 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

like image 727
Boon Yao Tan Avatar asked Jul 09 '15 08:07

Boon Yao Tan


People also ask

How do I count distinct occurrences in SQL?

To count the number of different values that are stored in a given column, you simply need to designate the column you pass in to the COUNT function as DISTINCT . When given a column, COUNT returns the number of values in that column. Combining this with DISTINCT returns only the number of unique (and non-NULL) values.

Can we use count with distinct in SQL?

Yes, you can use COUNT() and DISTINCT together to display the count of only distinct rows. SELECT COUNT(DISTINCT yourColumnName) AS anyVariableName FROM yourTableName; To understand the above syntax, let us create a table.

How can we count distinct records?

SELECT COUNT (DISTINCT item_num) FROM items; If the COUNT DISTINCT function encounters NULL values, it ignores them unless every value in the specified column is NULL. If every column value is NULL, the COUNT DISTINCT function returns zero (0).

How do I count the number of times a value appears in SQL?

The MySQL COUNT() function allows you to count how many times a certain value appears in your MySQL database. The function can also help you to count how many rows you have in your MySQL table. The function has one expression parameter where you can specify the condition of the query.


1 Answers

The easiest way is to shift your datetime (date,timestamp?) field back for 6 hours in a SQL statement and then you will get an interval in one day from 4AM to 12PM:

DISTINCT(DATE(DATE_SUB(dt,INTERVAL 6 HOUR)))

SQLFiddle demo

like image 123
valex Avatar answered Oct 19 '22 08:10

valex