SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Select query help


Select query help

Author
Message
Pink123
Pink123
SSC Journeyman
SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)

Group: General Forum Members
Points: 96 Visits: 136
I have a table Customer which has following columns

Custid,PersonId,Startdate,Enddate,name,age .......

For each person there can be multiple rows in the table customer depending
on startdate and end date .I want to select one personid for each person from table customer,the one having maximum end date.
Consider the following example for table Customer

CustId PersonId startdate Enddate Name age ...

100 1 01/12/2011 02/12/2011 person1

101 1 02/13/2011 04/14/2011 person1

102 2 02/11/2011 04/14/2011 person2

103 2 04/15/2011 05/12/2011 person2

My query should give me

101 1 02/13/2011 04/14/2011 person1

103 2 04/15/2011 05/12/2011 person2

Please provide some help as to how to form the query for such a scenario



Thanks in advance
Dan.Humphries
Dan.Humphries
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1129 Visits: 1212
Something like this should work nicely

Select 
t.CustId
,t.PersonId
,t.startdate
,t.Enddate
,t.Name
from
(
Select
CustId
,PersonId
,startdate
,Enddate
,Name
,ROW_NUMBER() OVER(PARTITION BY PersonId ORDER BY Enddate DESC) as rownum
from
table1
) as t
where
t.rownum=1



Dan

If only I could snap my figures and have all the correct indexes apear and the buffer clean and.... Start day dream here.
Brandie Tarvin
Brandie Tarvin
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14363 Visits: 8978
Another is to do an INNER JOIN to a subquery for the CustID and MAX(EndDate), joined on both fields.

A third method would be to put the above subquery into a CTE and join to the CTE.

Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
Pink123
Pink123
SSC Journeyman
SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)

Group: General Forum Members
Points: 96 Visits: 136
Thanks a lot
GSquared
GSquared
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23265 Visits: 9730
It looks to me like Customer and Person data need to be separated in this, to make it work well.

I'd be willing to be that "name" and "age" really belong in a "People" table.

Normalizing the data that way will make everything you do with it easier.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search