简体   繁体   中英

Mysql - count if not in the same day

I must extract a report from MySQL of how many times a client visited our office. The rule is to count a visit only if it has passed 24 from the last visitation.

If a client visits the office two times in one day, this should count as 1 (for the report).

However, the visit will be registered everytime in the database and I cannot change the database or create new tables. I must work with what I have.

Follows a SQL Fiddle of my scenario:

The outuput should be two visits were done in 29/aug, one visit done in 03/sept.

http://sqlfiddle.com/#!2/fc7f5/2/0

Could someone please put me in the right direction to archive this? I googled as much as I could and did not find the right answer.

Thank you very much in advance for your time and help.

Looks like you need to use GROUP BY :

select date(visit_date), count(*) 
from visits 
where client_id =1 
group by date(visit_date)

Notes:

  • You may or may not need to convert visit_date to a date in the group by -- depends on whether it stores the time of the visit or just the date.
  • Also, you may need to use count distinct(client_id) since you mention not counting the same client twice in the same day. A little unclear from your question.

If you use select visit_date,count(*) from visits where client_id=1 group by visit_date You'll see that it now reports 2 visits for 29/aug and one for 03/sept.

Count the number of rows returned to find out how many visits the patient has had.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM