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


How do I get the latest record in each month for each customer?


How do I get the latest record in each month for each customer?

Author
Message
siewlian908
siewlian908
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 36
How do I get the latest record in each month for each customer? Eg:

TableA
ID Value PerformedDtm
111 10 15/02/2008
121 15 15/02/2008
121 20 25/03/2008
131 20 09/12/2008
141 16 09/12/2008
131 18 25/12/2008
111 17 27/12/2008
121 16 29/12/2008


expected result:
ID Value PerformedDtm
111 17 27/12/2008
121 16 29/12/2008
131 18 25/12/2008
141 16 09/12/2008
nitinpatel31
nitinpatel31
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1411 Visits: 293
I am not sure what exactly you want? Do you want latest record for Id or latest record for each month? From your example it seems you want latest record for each Id. If it is the case then below is your solution.

CREATE TABLE #T (Id INT, Val INT, PerformedDtm DATE)

INSERT INTO #t (Id, Val, PerformedDtm)
SELECT 111 Id ,10 Val ,CAST('02/15/2008' AS DATE) PerformedDtm
UNION ALL SELECT 121 ,15 ,'02/15/2008'
UNION ALL SELECT 121 ,20 ,'03/25/2008'
UNION ALL SELECT 131 ,20 ,'12/09/2008'
UNION ALL SELECT 141 ,16 ,'12/09/2008'
UNION ALL SELECT 131 ,18 ,'12/25/2008'
UNION ALL SELECT 111 ,17 ,'12/27/2008'
UNION ALL SELECT 121 ,16 ,'12/29/2008'



SELECT #T.* FROM #T
INNER JOIN (SELECT Id, MAX(PerformedDtm) PerformedDtm FROM #T GROUP BY Id) AS T ON T.Id = #T.Id AND T.PerformedDtm = #T.PerformedDtm
ORDER BY #T.Id

DROP TABLE #T

Regards,
Nitin
robert.dickens
robert.dickens
SSC Rookie
SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)

Group: General Forum Members
Points: 39 Visits: 25
why not try this

select max(id) as id , -- latest primary key column
customer , -- customer key value
max(event_date) as event_date -- latest day of the month
from mytable
group by customer , -- base criteria
datepart(year,event_date) , -- group by year
datepart(month,event_date) -- group by month

the output of this query will give a a single row for each customer for each year and month
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