views:

41

answers:

4

I've got a simple table "Logins" with two columns:

  1. username (nvarchar)
  2. logged (datetime)

It's really simple, just records the username and datetime when someone logs into my web application. Sometimes, however, people login a few times in one minute... I want to try to run a query to filter those results and only have it return one row even if there are multiple logins in the same minute.

Here is an example:

(Results I get with a simple select)

username  logged
-------------------
kh0013    2010-08-16 21:29:21.020
tmt0006   2010-08-16 21:24:16.030
jrc0014   2010-08-16 21:17:37.187
jrc0014   2010-08-16 21:17:15.043
jrc0014   2010-08-16 21:17:00.593
jrm0017   2010-08-16 20:52:57.673
as0044    2010-08-16 20:45:51.210
snb0006   2010-08-16 20:33:29.873
weo0021   2010-08-16 19:54:57.093

As you can see, the user "jrc0014" logged in multiple times within the same minute. How can I write a query so that user is only logged once, like this:

(Desired Results)

username  logged
------------------
kh0013    2010-08-16 21:29:21.020
tmt0006   2010-08-16 21:24:16.030
jrc0014   2010-08-16 21:17:00.593
jrm0017   2010-08-16 20:52:57.673
as0044    2010-08-16 20:45:51.210
snb0006   2010-08-16 20:33:29.873
weo0021   2010-08-16 19:54:57.093
A: 

IN SQL Server, floor the datetime to the minute like this:

SELECT DISTINCT
    username , DATEADD(minute,DATEDIFF(minute,0,logged),0) AS logged
    FROM YourTable
KM
A: 

Assuming SQL Server.

select username, logged
FROM logins l
WHERE NOT EXISTS(SELECT * FROM logins l2 
                 where l.username =l2.username 
                 AND DATEDIFF(SECOND,l2.logged,l.logged) < 60)
Martin Smith
For some reason, this query did not return any rows either. The above query by OMG Ponies did work, however.
strazz
A: 
select username, logged
  from Logins o
 where not exists(select 1 
                    from Logins i
                   where i.username = o.username
                     and datediff(second, i.logged, o.logged) <= 60)
                 )
Frank
For some reason, this query did not return any rows. The above query by OMG Ponies did work, however.
strazz
A: 

To get the output you listed, use:

  SELECT yt.username , 
         MAX(yt.logged) AS logged
    FROM YOUR_TABLE yt
GROUP BY yt.username, DATEADD(minute,DATEDIFF(minute,0,logged),0)
OMG Ponies
Thank you for your rapid response. This query worked great. I know it's not in my question but a couple of responses below gave me the idea that the "1 minute" could be customized. In other words, I could feed in a value for the number of seconds the records can be apart from each other to determine whether or not to show them. In your query here, is it hard-coded to one minute?
strazz
@strazz: SQL should be considered "hard coded" - there isn't much it supports that is "dynamic" without actually use dynamic SQL syntax. If you want to support timeframes other than one minute, you have to tweak the `GROUP BY`, the DATEADD portion specifically to group according to your desired time frame.
OMG Ponies
Thanks for your rapid reply... would you mind giving me an example modification for 30 seconds and 90 seconds? That would help me understand the query fully and I'll be good to go! I'm also marking your post as the answer. Thanks again.
strazz
@strazz: I'd recommend asking a new question for that - the syntax isn't likely to identical, and it gives others a chance to answer too.
OMG Ponies