1

I want the MAX(Date) for each distinct ID. This is easy:

SELECT
    Id,
    MAX(LastModifiedDate)
FROM 
    [MyServer].[Database].[Table]
GROUP BY
    Id;

but I also need to include other columns like first name, last name, company, etc. which cannot be aggregated, and it won't work if I put them in the GROUP BY clause, because then it returns rows I don't want.

2
  • You can't. That's invalid SQL, as in SQL the language. What values should those fields return? Commented Jun 28, 2018 at 8:35
  • Please explain what you want to do in the question itself. Post what you want to return, what you want the data to look like. Not how you think the solution would look. Most likely you need an OVER() clause in the aggregate functions instead of a GROUP BY Commented Jun 28, 2018 at 8:45

5 Answers 5

3

Use that query to filter with the same table by ID and that last modified date.

SELECT
    T.*
FROM
    [MyServer].[Database].[Table] AS T
    INNER JOIN (
        select Id,
        MAX(LastModifiedDate) AS  MaxLastModifiedDate
        FROM [MyServer].[Database].[Table]
        group by Id
    ) AS N ON 
        T.Id = N.Id AND
        N.MaxLastModifiedDate = T.LastModifiedDate

EDIT: If you have multiple max dates for each ID, then you can't use their value to filter. You can use a ROW_NUMBER to rank them instead:

;WITH MaxByRowNumber AS
(
    SELECT
        T.*,
        LastModifiedDateRanking = ROW_NUMBER() OVER (PARTITION BY T.ID ORDER BY T.LastModifiedDate DESC)
    FROM 
        [MyServer].[Database].[Table] AS T
)
SELECT
    M.*
FROM
    MaxByRowNumber AS M
WHERE
    M.LastModifiedDateRanking = 1

You might want to add another column to the ORDER BY to untie the most recent updated dates, or not, depending on your needs.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried this and my inner query returns 4,155, but the full query returns 4,876 records. I then copied the results of the outer query to Tableau and indeed I have 4,155 distinct ID but only 3,249 distinct LastModifiedDate (which is accurate to the second). Any ideas on how to debug this?
@OligarchicTendencies this can happen if you have the same ID with 2 or more records with the same MaxLastModifiedDate. I've edited the answer to use another way around.
1

In order to do this, instead of using group by, try over():

select Id, <any other thing you want>,max(LastModifiedDate) over (partition by Id) as LastModifiedDate
FROM [MyServer].[Database].[Table]; 

2 Comments

Why the GROUP BY ? This will prevent the use of non-group fields in the SELECT clause
Swstos Panagioth, I accidentally copy-pasted it from the original
0

One way would be using a CTE:

with cte as 
(
  select Id,
  max(LastModifiedDate) LastModifiedDate
  FROM [MyServer].[Database].[Table]
  group by Id
)
select t.*, c.LastModifiedDate
from [MyServer].[Database].[Table] t join cte c on t1.id = cte.id

Comments

0

Figured this out literally as soon as I posted it. I was just confused on the proper way to use a window function.

SELECT [Id]
  ,LastModifiedDate
  ,Row_Number() over (Partition by Id order by LastModifiedDate DESC) as rn
  ,[FirstName]
  ,[LastName]
  ,[Company]
  ,[Title]     
FROM [MyServer].[Database].[Table] as C
where rn = 1

What i was trying to do was:

,Row_Number() over (Partition by Id order by MAX(LastModifiedDate) DESC) as rn

but MAX makes no sense because it is DESC

8 Comments

This looks nothing like what you asked. If you want to return the latest modification date for an ID use MAX(LastModifiedDate) OVER (PARTITION by ID).
Yea, now it isn't even running... :( C.rn invalid column name??
But MAX(LastModifiedDate) brings me back to my original issue. There are several answers here, I will try one
Meaning what? MAX has an OVER clause just like most aggregate functions.. The examples in the docs show how to use MAX with an OVER clause without GROUP BYs
Update the question and explain what you want, not how you think it can be done. "I want a user's details and a field showing the latest modification date" is what. GROUP BY or MAX(LastModificationDate) OVER (PARTITION BY ID) is how. If you get duplicate rows, explain that in the question as well. That's why you should provide examples of the input data and desired output too
|
0

You can use subquery :

select t.*
from table t
where LastModifiedDate = (select max(t1.LastModifiedDate) 
                          from table t1 
                          where t1.id = t.id
                         );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.