Author |
Topic |
xxhajime08xx
Starting Member
4 Posts |
Posted - 2013-11-06 : 20:36:53
|
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
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-11-06 : 23:34:30
|
SELECT ReferenceNo,LastName,FirstName,MiddleInit,DatePostedFROM (SELECT ReferenceNo,LastName,FirstName,MiddleInit,DatePosted, ROW_NUMBER() OVER(PARTITION BY LastName,FirstName,MiddleInit ORDER BY DatePosted DESC) RN FROM TabbleName) tempWHERE temp.RN=1--Chandu |
|
|
xxhajime08xx
Starting Member
4 Posts |
Posted - 2013-11-07 : 01:47:03
|
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 explanationFrom 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 ) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-07 : 07:36:41
|
[code]DECLARE @StartDate datetimeSET @StartDate='2013-11-06 08:00' -- pass any value you want hereSELECT ReferenceNo,LastName,FirstName,MiddleInit,DatePostedFROM TabbleName WHERE DatePosted> = @StartDateAND DatePosted < DATEADD(minute,31,@StartDate)AND EXISTS (SELECT 1FROM TableNameWHERE LastName = t.LastNameAND FirstName = t.FirstNameand MiddleInit = t.MiddleInitAND DatePosted < @StartDate)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
xxhajime08xx
Starting Member
4 Posts |
Posted - 2013-12-09 : 03:01:57
|
Hi is it also possible to show both ReferenceNo including LastName,FirstName,MiddleInit,DatePosted where LastName,FirstName,MiddleInit has duplicate? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-12-09 : 06:54:25
|
do you mean this?DECLARE @StartDate datetimeSET @StartDate='2013-11-06 08:00' -- pass any value you want hereSELECT t1.ReferenceNo,t1.LastName,t1.FirstName,t1.MiddleInit,t1.DatePosted,t2.ReferenceNo AS prevReferenceNo, t2.DatePosted AS prevDatePostedFROM TabbleName t1INNER JOIN TabbleName t2ON t1.LastName = t2.LastNameAND t1.FirstName = t2.FirstNameand t1.MiddleInit = t2.MiddleInitAND t2.DatePosted < @StartDateWHERE t1.DatePosted > = @StartDateAND 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 MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
xxhajime08xx
Starting Member
4 Posts |
Posted - 2013-12-09 : 21:13:16
|
Thanks a lot visakh16 for the help just the query i need to get started. ^_^ |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-12-09 : 22:33:01
|
you're welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|