Solved

SQL select max time from accessed table for each object in a list

Posted on 2014-12-09
4
156 Views
Last Modified: 2014-12-09
I am trying to write a query which, based on a set of IDs, gives me the latest time for that ID from another table. This is basically showing users who accessed an item. the users are stored in one table, while the access logs are stored in another.

I want to be able to provide a list of ids (where user.id in (xyx) ) and get one row representing the latest time from the accessed table. Can someone help? i have a query right now which shows an extract for all users and accesses within a time range. I think this can be modified but i cant figure it out.

select distinct
AccessLog.ACCESSED as   time_accessed,
AccessLog.ACCESSTYPE     as access_type,
AccessLog.USERID    as user_id,
USERS.Name as USERNAME
from AccessLog 
join USERS on AccessLog.USERID =  USERS.id 
where accessed between  '2014-11-12 11:00:00'  and  '2014-11-25 10:00:00'  
order by accessed                                                                                             
                                                                                           

Open in new window

0
Comment
Question by:mikegrad7
4 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40489366
>based on a set of IDs, gives me the latest time for that ID from another table.
> the users are stored in one table, while the access logs are stored in another.
Not really following your code block, so I'll just whip this up based on the above statements and let you figure it out.   Subquery to get the max login times for every user, the a main query to reference the id's you want in the return set, LEFT JOIN on the subquery.
SELECT one.id, two.max_date
FROM one_table one
   LEFT JOIN (
      SELECT id, MAX(date_column) as max_date
      FROM another_table 
      GROUP BY id) two ON one.id = two.id
ORDER BY one.id

Open in new window

0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40489367
you may want to read this article:
http://www.experts-exchange.com/Database/Miscellaneous/A_3203-DISTINCT-vs-GROUP-BY-and-why-does-it-not-work-for-my-query.html
it will explain the "issue" and the path to the working SQL
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 40489421
try this:

select * from (
 select l.ACCESSED as time_accessed, l.ACCESSTYPE  as access_type, l.USERID as user_id, u.Name as USERNAME,
 row_number() over (partition by u.userid order by l.accessed desc) as rn
 from AccessLog l
 inner join USERS u on l.USERID =  u.id
 where accessed between  '2014-11-12 11:00:00'  and  '2014-11-25 10:00:00'
) x where rn=1

Open in new window

0
 
LVL 1

Author Closing Comment

by:mikegrad7
ID: 40489475
great, thanks for the quick feedback!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Installing SQL Server Express Management Studio 4 23
CROSS APPLY 4 43
CRM 2011 How to Create a Quote with same QuoteID and QuoteNumber 4 46
SQL Error in WHERE Clause 5 38
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

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

14 Experts available now in Live!

Get 1:1 Help Now