Filter dates when user entered our system

Asked

Viewed 63 times

0

I am trying to filter the dates when users connect to my system.

Have a however, first need to filter so there are no duplicate users.

I’m already doing it using the argument DISTINCT in my query, with the result that my query is returning, I want to search in the column DATE, to know every day that users have connected to my site.

I’m using the following command:

SELECT
    *
FROM
    wps_history
WHERE 
    inserted
BETWEEN "2016-11-01 00:00:00" AND "2016-11-30 23:59:59"

This command brings me all the results, but this is giving more than 40,000 lines, performing the filter DISTINCT the result and filtered to 500 lines, I am using the following command:

SELECT DISTINCT
    user_id
FROM
    wps_history
WHERE
    inserted
BETWEEN
    "2016-11-01 00:00:00" AND "2016-11-30 23:59:59"

Now I’m wondering how I can use the above command and bring up the dates on which each USER_ID connected to my system.

1 answer

1


Do not use the DISTINCT for this, use GROUP BYby user code:

If you want the last date:

SELECT user_id,
       MAX(inserted)
  FROM wps_history
 WHERE inserted BETWEEN '2016-11-01 00:00:00' AND '2016-11-30 23:59:59'
 GROUP BY user_id

If you want the first date:

SELECT user_id,
       MIN(inserted)
  FROM wps_history
 WHERE inserted BETWEEN '2016-11-01 00:00:00' AND '2016-11-30 23:59:59'
 GROUP BY user_id

If you want to group everything in one column:

SELECT user_id,
       GROUP_CONCAT(DATE_FORMAT(inserted, '%d-%m-%Y') separator ', ') as datas
  FROM wps_history
 WHERE inserted BETWEEN '2016-11-01 00:00:00' AND '2016-11-30 23:59:59'
 GROUP BY user_id
  • Hello, Sorack, thank you very much. But I need all dates within the 30-day period. How can I adapt the code so that it returns all the dates you hear connection during the 30 days. Thanks for the (GROUP BY) tip. I’m crawling in the programming and I’m really enjoying it! Valeu

  • @Cicero dispose. If the answer has solved your problem do not forget to mark to accept it. This way you can use it if someone has a similar question!

  • It helped me, but it didn’t completely solve my problem, because I need all the dates that each user_id connected to in the 30-day period. The GROUP BY tip was already a novelty for me, Thanks anyway!

  • @Cíceroalves specify in the question which database you are using so I can supplement my answer

  • @Cicero tries to BETWEEN GETDATE() -30 AND GETDATE(). Sql-Server.

  • @Sorack, I use Mysql. The column of the table "WPS_HISTORY" that records whenever a user connects is "DATA", I want to know which users "USER_ID", connected within 30 days. Thanks for your attention, if you can recommend me some books to read on the subject I will be grateful!

  • @Cicero you want to show all dates in one column?

  • @Sorack the display can be either way

  • @Marconi, thank you very much.

  • @Cicero take a look at the answer I added at the end now

  • 1

    @Sorack thank you so much! Now it’s gone!

Show 6 more comments

Browser other questions tagged

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