Solved

SQL statement count logins by date

Posted on 2014-03-12
6
359 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

744 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

11 Experts available now in Live!

Get 1:1 Help Now