Solved

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

Posted on 2014-12-09
4
154 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

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

10 Experts available now in Live!

Get 1:1 Help Now