Solved

SQL statement count logins by date

Posted on 2014-03-12
6
360 Views
Last Modified: 2014-03-12
Hi, I am trying to run a SQL query to measure user performance. Each time a student logs into the intranet a new record with a date time stamp is created.

I want to run a query that will return the student ID their very first login date&time their latest login date&time and then a count of how many times they have logged in.

I have tried the below statement but it's just returning the dates incorrectly  and not the counts
SELECT *
FROM (SELECT Student_ID, Time_stamp,
      MAX(Time_stamp) OVER (PARTITION BY Student_ID) as MaxLoginDate
FROM Intranet_LogIn) x
WHERE Time_stamp = MaxLoginDate
 
 My results should look like this
Student_ID         FirstLogin                                             LatestLogin                                         LoginCounts
 
00223                    2013-12-24 17:05:35.000                                2014-01-08 13:12:12.000                10
78143                    2013-12-26 11:05:11.000                                2014-02-09 16:11:45.000                8
99233                    2013-12-27 13:05:23.000                                2014-02-10 12:12:21.000                3
00323                    2013-12-28 12:06:34.000                                2014-01-11 17:22:33.000                3
00314                    2013-12-29 12:07:45.000                                2014-03-11 13:36:12.000                4
0
Comment
Question by:jay_eire
  • 2
  • 2
  • 2
6 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 39923224
what about this:
SELECT Student_ID, Time_stamp Last_Login_Time, cnt number_of_logins  
FROM ( SELECT Student_ID, Time_stamp
     , ROW_NUMBER() OVER (PARTITION BY Student_ID ORDER BY  Time_stamp DESC) as RN
     , COUNT(*) OVER  (PARTITION BY Student_ID ) as CNT
FROM Intranet_LogIn
  ) sq
WHERE sq.rn = 1 

Open in new window

0
 
LVL 22

Assisted Solution

by:Snarf0001
Snarf0001 earned 250 total points
ID: 39923257
Any reason not to use a standard group by?

select Student_ID, min(Time_stamp) as FirstLogin, max(Time_stamp) as LastLogin, count(1) as TotalLogins
from Intranet_LogIn
group by Student_ID

Open in new window

0
 
LVL 7

Author Comment

by:jay_eire
ID: 39923339
Thanks for the replies guys!

I did not know I could achieve the same result using group by.

Both solutions work!

OK to split the points?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39923356
ok for me
0
 
LVL 22

Expert Comment

by:Snarf0001
ID: 39923407
No problem.
0
 
LVL 7

Author Closing Comment

by:jay_eire
ID: 39923516
Thanks again
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now