Link to home
Start Free TrialLog in
Avatar of KOvitt
KOvittFlag for United States of America

asked on

Get last available day of year, from daily data group by year

Hello,
I have a bunch of daily data going back to 1993. I need to pull specific data from the last available day for each year (not always YYYY-12-31) and group by year.

Any tips on how I can achieve this? I know I can use MAX to get the latest day but I don't now how to do that for each individual year in one table.

Thanks!
Avatar of John_Arifin
John_Arifin
Flag of Indonesia image

DECLARE @PROC_YEAR INT
SET @PROC_YEAR = 1993
WHILE  @PROC_YEAR < 2012
      BEGIN
            SELECT @PROC_YEAR AS THE_YEAR, MAX(MY_DATE)AS TE_DATE FROM MY_TABLE WHERE YEAR(MY_DATE) = @PROC_YEAR
            SET @PROC_YEAR = @PROC_YEAR + 1
      END
ASKER CERTIFIED SOLUTION
Avatar of MacAnthony
MacAnthony
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KOvitt

ASKER

Haha wow, I apologize for wasting your time. I for some reason thought that wouldn't work and didn't bother to try. Thank you!
No problem at all. Glad I could help.