Solved

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

Posted on 2014-12-09
4
162 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
[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
4 Comments
 
LVL 66

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 143

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 55

Expert Comment

by:Huseyin KAHRAMAN
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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

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…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

690 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