Question

I have this SQL query, that returns just one single column from a table. This column holds many dates in it, Ex: '05/09/2014', etc.

I would like to also return a second column that has the value of the last computed date it finds from the first (this column will only have one row, or possibly multiple rows all with the same value, either way is fine.

I can't seem to get the LAST() or LAST_VALUE() functions to be able to do this seemingly simple task for me. Any suggestions? Here's my query:

--The converts are just to get the dates in a format I like...
SELECT CONVERT(varchar(10), WeekEnding, 101) WeekEnding,   LAST_VALUE(CONVERT(varchar(10), WeekEnding, 101)) OVER(ORDER BY WeekEnding)
FROM dbo.Weekly 

I'm not sure how exactly I should be using the OVER clause.

Was it helpful?

Solution

MAX should do:

SELECT  CONVERT(varchar(10), WeekEnding, 101) WeekEnding,   
        CONVERT(varchar(10), MAX(WeekEnding) OVER(), 101) MaxWeekEnding
FROM dbo.Weekly 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top