Solved

Help with SQL Syntax - return all records of last date visited

Posted on 2009-07-09
9
248 Views
Last Modified: 2012-06-27
Without using TOP or ROW_NUMBER()  (because each person has a different record amount of times they visited) how can I return the last date visited.

EXAMPLE:
As of today July 09, 2009 , I would to look back at the last visited date and return all rows of records for their last date of visit.

VisitKey       Date of Service
    11            07/01/2009
    12            07/01/2009
    13            07/01/2009
    14            07/01/2009
    15            07/05/2009
    16            07/05/2009
    17            07/05/2009
0
Comment
Question by:Say55ow
[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
  • 4
9 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24817490
Why would ROW_NUMBER() not work for you here?  Doesn't matter the number of records, if you ORDER BY date_of_service DESC and take row = 1 then will always be the last visit whether a person has one or a thousand visits.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24817531
If a person is represented by a visit key then would look something like this (can replace visit key with proper column for person id to get per person last date of service).
-- without row_number()
SELECT visitkey, max(date_of_service) as lastVisitDate
FROM your_table_name
GROUP BY visitkey;
 
-- with row_number()
SELECT visitkey, date_of_service as lastVisitDate
FROM (
   SELECT *, row_number() OVER(PARTITION BY visitkey ORDER BY date_of_service DESC) AS row
   FROM your_table_name
) a
WHERE a.row = 1;

Open in new window

0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 400 total points
ID: 24817545
If your date of service does not have the time (datetime field with 00:00:00 time)....


select * from SomeTable
where Date_Of_Service = (select max(Date_Of_Service) from SomeTable)

Open in new window

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 17

Expert Comment

by:pssandhu
ID: 24817551
It would be nice to see the table structure but letss assume you have a Person_ID, VistitKey, and DateVisited as your columns. To get all records for each person for thier last visit data you can do something like this:
Select  t.*
From   YourTable t INNER JOIN (Select Person_ID, MAX(DateVisited) From YourTable Group by Person_ID) l
             ON t.Person_ID = l.Person_ID
P.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24817591
After reading the question again, I believe I misread the requirement.
0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 100 total points
ID: 24817640
And I see now why this didn't make sense to me about row_number().  You want to pull back rows of data, the sample you gave is not finished product... :( I need to read better.

I would do this like Brandon showed or using INNER JOIN; however, slight syntax correction to what P. posted.
SELECT t.*
FROM your_table_name t
INNER JOIN (SELECT Person_ID, max(Date_Of_Service) AS max_DOS FROM your_table_name GROUP BY Person_ID) l
   ON t.Person_ID = l.Person_ID AND t.Date_Of_Service = l.max_DOS

Open in new window

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24817661
LOL, it happens!
I still misread what you wrote Brandon.  Thought you nailed it.  Think you are just missing the link on user.
select * 
from SomeTable t1
where Date_Of_Service = (select max(Date_Of_Service) from SomeTable t2 where t2.person_id = t1.person_id)

Open in new window

0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24817671
you can always use dense_rank() instead of row_number().  Dense_Rank will return the same number for duplicate values.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24817684
mwvisa1:

Yes.  That is what I was missing.  I used a subselect instead of a correlated subselect.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

696 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