Solved

SQL statement count logins by date

Posted on 2014-03-12
6
365 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.

688 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