skip to Main Content

I have a dataframe like this

Value. Date
A 08/08/2009
A 09/12/2021
A 05/10/2022
A 06/09/2022
A 07/08/2022

I need output like

VALUE DATE
A 05/10/2022
A 06/09/2022
A 07/08/2022

We have to print a latest year with all month data present in the date column .please refer output table.

i used SQL query like

Select Top 10 * from table where
Order by (Date) DESC;

The max() select only one date so that didn’t help me

But didn’t get expected answer.
Can please someone help me with the query ?

2

Answers


  1. You can just use MAX in a subquery, this will produce the intended outcome you have shown in your question:

    SELECT yourcolumn
    FROM yourtable
    WHERE 
    YEAR(yourcolumn) = (SELECT MAX(YEAR(yourcolumn)) FROM yourtable);
    

    The latest year is 2022, so MAX in the subquery will find this year and the whole query will select all dates in 2022.

    Login or Signup to reply.
  2. SELECT *
    FROM tablename
    WHERE datecolumn >= (SELECT DATE_FORMAT(MAX(datecolumn), '%Y-01-01')
                         FROM tablename)
    

    To improve this query you’d have an index by datecolumn (or where this column is an expression prefix).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search