SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Help with queries
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

xxhajime08xx
Starting Member

Philippines
4 Posts

Posted - 11/06/2013 :  20:36:53  Show Profile  Reply with Quote
Hi I have a table (with fields ReferenceNo,LastName,FirstName,MiddleInit,DatePosted) what i would like to do is to get the latest ReferenceNo and DatePosted where the LastName,FirstName, and MiddleInit is already existing.

Can anyone help me formulate a query for this?

bandi
Flowing Fount of Yak Knowledge

India
2224 Posts

Posted - 11/06/2013 :  23:34:30  Show Profile  Reply with Quote
SELECT ReferenceNo,LastName,FirstName,MiddleInit,DatePosted
FROM (SELECT ReferenceNo,LastName,FirstName,MiddleInit,DatePosted, ROW_NUMBER() OVER(PARTITION BY LastName,FirstName,MiddleInit ORDER BY DatePosted DESC) RN FROM TabbleName) temp
WHERE temp.RN=1


--
Chandu
Go to Top of Page

xxhajime08xx
Starting Member

Philippines
4 Posts

Posted - 11/07/2013 :  01:47:03  Show Profile  Reply with Quote
Thanks Chandu and sorry if I did'nt explain further however i would only like to get those records with more than one record. below is a more detailed explanation

From a 30 minute duration (say 11-06-2013 08:00:00.000 to 11-06-2013 08:30:00.999) I would like to get those ReferenceNo and DatePosted where the LastName,FirstName, and MiddleInit is already existing (records below 11-06-2013 07:59:59.999 )

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 11/07/2013 :  07:36:41  Show Profile  Reply with Quote

DECLARE @StartDate datetime
SET @StartDate='2013-11-06 08:00' -- pass any value you want here

SELECT ReferenceNo,LastName,FirstName,MiddleInit,DatePosted
FROM TabbleName 
WHERE DatePosted> = @StartDate
AND DatePosted < DATEADD(minute,31,@StartDate)
AND EXISTS (SELECT 1
FROM TableName
WHERE LastName = t.LastName
AND FirstName = t.FirstName
and MiddleInit = t.MiddleInit
AND DatePosted < @StartDate
)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs

Edited by - visakh16 on 11/07/2013 07:38:08
Go to Top of Page

xxhajime08xx
Starting Member

Philippines
4 Posts

Posted - 12/09/2013 :  03:01:57  Show Profile  Reply with Quote
Hi is it also possible to show both ReferenceNo including LastName,FirstName,MiddleInit,DatePosted where LastName,FirstName,MiddleInit has duplicate?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 12/09/2013 :  06:54:25  Show Profile  Reply with Quote
do you mean this?


DECLARE @StartDate datetime
SET @StartDate='2013-11-06 08:00' -- pass any value you want here

SELECT t1.ReferenceNo,t1.LastName,t1.FirstName,t1.MiddleInit,t1.DatePosted,
t2.ReferenceNo AS prevReferenceNo, t2.DatePosted AS prevDatePosted
FROM TabbleName t1
INNER JOIN TabbleName t2
ON t1.LastName = t2.LastName
AND t1.FirstName = t2.FirstName
and t1.MiddleInit = t2.MiddleInit
AND t2.DatePosted < @StartDate
WHERE t1.DatePosted > = @StartDate
AND t1.DatePosted < DATEADD(minute,31,@StartDate)


Beware that in this case if there are multiple prev reference records you'll get multiple records in output for the same LastName,FirstName, and MiddleInit combination.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

xxhajime08xx
Starting Member

Philippines
4 Posts

Posted - 12/09/2013 :  21:13:16  Show Profile  Reply with Quote
Thanks a lot visakh16 for the help just the query i need to get started. ^_^

Edited by - xxhajime08xx on 12/10/2013 00:36:03
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 12/09/2013 :  22:33:01  Show Profile  Reply with Quote
you're welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000