Solved

SQL statement count logins by date

Posted on 2014-03-12
6
363 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 143

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 23

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 143

Expert Comment

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

Expert Comment

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

Author Closing Comment

by:jay_eire
ID: 39923516
Thanks again
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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.

730 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