How to count weekly single accesses in Mysql

Asked

Viewed 64 times

0

I have this query in Mysql to check the unique accesses to a system of mine:

SELECT COUNT(DISTINCT id_usuario) AS total, dia FROM acesso
WHERE (dia BETWEEN '2018-01-21' AND '2019-01-21')
GROUP BY dia
ORDER BY dia ASC

And the way I see it, he’s listing the unique access to the system.

My doubt: the customer asked to check the weekly single accesses, IE, if the user access at least 1 only time during the period of 1 week, would already count his access, how to do? I tried this way, but the total ends up giving less than the only daily access:

SELECT COUNT(DISTINCT id_usuario) AS total, dia FROM acesso
WHERE (dia BETWEEN '2018-01-21' AND '2019-01-21')
GROUP BY WEEK(dia)
ORDER BY dia ASC
  • What version of MySQL?

  • Version of the database client: libmysql - mysqlnd 5.0.11-dev - 20120503 - $Id: 76b08b24596e12d4553bd41fc93cccd5bac2fe7a $

2 answers

0

If you do this query, you will create an analytical table with the distinct ones in a given week, filtering by date range

SELECT id_usuario, week(dia) as semana FROM acesso
WHERE dia  BETWEEN '2019-01-01' and '2019-04-16'
group by id_usuario, week(dia)

Based on these results, you can count the distinct ones in this time interval

SELECT count(distinct id_usuario) as total from (SELECT id_usuario, week(dia) as semana FROM acesso
WHERE dia  BETWEEN '2019-01-01' and '2019-04-16'
group by id_usuario, week(dia)) T
  • Here is the error: <b>Warning</b>: mysql_fetch_array() expects Parameter 1 to be Resource, Boolean Given in <b>C: xampp htdocs Pca_brasilprev_blog Dashboard.php</b> on line <b>56</b> LINE 56 is where the Mysql query is

  • 1

    @Bins, you don’t need to add multiple answers. If one of the answers didn’t work, just edit it, fix the problems and add information as needed. Editing posts is a common practice and even encouraged on the site: https://answall.com/help/editing

-1

If you want to know only the total of accesses in the last 7 days, you can do so:

SELECT COUNT(DISTINCT id_usuario) AS total FROM acesso
WHERE dia  >= (DATE(NOW()) - INTERVAL 7 DAY)

  • No... I need to know on any date. Example: from 2017 until 2019.... here I list the weeks... and next to how many unique accesses

Browser other questions tagged

You are not signed in. Login or sign up in order to post.