Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Last value entered based on last modified date


Last value entered based on last modified date

Author
Message
Adelphi
Adelphi
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 66
I need to get the last modified value for each project and each owner

-- SAMPLE DATA
DECLARE @Audit TABLE (
   [ID] varchar(50),
   [Owner] varchar(50),
   [Modified on] datetime,
   [Value] money
)

SET DATEFORMAT dmy

INSERT INTO @Audit
SELECT 'ProjA','OwnerA','20/05/2011 17:45','1256'
UNION ALL SELECT 'ProjA','OwnerA','11/07/2011 16:22','2540'
UNION ALL SELECT 'ProjB','OwnerB','21/06/2011 09:32','25610'
UNION ALL SELECT 'ProjB','OwnerB','21/06/2011 09:37','30000'
UNION ALL SELECT 'ProjB','OwnerB','21/06/2011 09:40','45000';

-- SOLUTION
WITH AuditData AS (
   SELECT *,
    RN = ROW_NUMBER() OVER(PARTITION BY ID ORDER BY [Modified On] DESC),
    max([Modified on]) OVER(PARTITION BY ID) AS [Max Modified On]
   FROM @Audit
)
SELECT [ID], [Owner], [Modified on], [Value], [Max Modified On]
FROM AuditData AS AD

I need the following result:

[ID], [Owner], [Modified on], [Value], [Max Modified On]
ProjA   OwnerA   2011-07-11 16:22:00.000   2540.00   2011-07-11 16:22:00.000
ProjB   OwnerB   2011-06-21 09:40:00.000   45000.00   2011-06-21 09:40:00.000

Instead I get the following:

ProjA   OwnerA   2011-05-20 17:45:00.000   1256.00   2011-07-11 16:22:00.000
ProjA   OwnerA   2011-07-11 16:22:00.000   2540.00   2011-07-11 16:22:00.000
ProjB   OwnerB   2011-06-21 09:32:00.000   25610.00   2011-06-21 09:40:00.000
ProjB   OwnerB   2011-06-21 09:37:00.000   30000.00   2011-06-21 09:40:00.000
ProjB   OwnerB   2011-06-21 09:40:00.000   45000.00   2011-06-21 09:40:00.000


What am I doing wrong?

Your help is appreciated.

Many Thanks

A
Koen Verbeeck
Koen Verbeeck
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16366 Visits: 13199

WITH AuditData AS (
SELECT *,
RN = ROW_NUMBER() OVER(PARTITION BY ID ORDER BY [Modified On] DESC),
max([Modified on]) OVER(PARTITION BY ID) AS [Max Modified On]
FROM @Audit
)
SELECT [ID], [Owner], [Modified on], [Value], [Max Modified On]
FROM AuditData AS AD
WHERE RN = 1;





How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at SQLKover.

MCSA SQL Server 2012 - MCSE Business Intelligence
Phil Parkin
Phil Parkin
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8300 Visits: 19448
Or this maybe:

with MaxDates as (select id, MaxMod = max([Modified on]) from @Audit a group by id)
select a.id,a.Owner, a.[Modified on],a.Value
from @Audit a
join MaxDates m on a.id = m.id and a.[Modified on] = m.MaxMod




Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
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