简体   繁体   中英

How can I see how many times a person came per month (SQL)

For my stystem I want to know how many times a visitor came to my shop. I got wifi sensors and they get alot of addresses and I want to know how many times the visitors came in a month

This is the database I use (time is in unix time and get fixed with FROM_UnixTime(sensordata1.time)

So what I want to get is a the address with the number of visits last month.(per day not per address so if he came 5times a day count it as 1)

You want to see March 2017. So restrict your results to March 2017 in the WHERE clause. You want one result row per visitor (address). So GROUP BY address. You want to count each day just once. So COUNT DISTINCT days.

select 
  address,
  count(distinct from_unixtime(sensordata1.time, '%Y-%m-%d'))
from sensordata1 
where from_unixtime(sensordata1.time, '%Y-%m') = '2017-03'
group by address;

If you want this more flexible, ie always the last month when executing the query instead of 2017-03 fixed, then find today, subtract a month, and take the month got thus.

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