Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL Query to fetch rows from recent year

I need to retrieve the rows based on last recent year.

Table Structure

I’ve attached the table structure, I need to retrieve records from most recent year for each customer_website

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here in the table, amazon.com is repeating 3 times where, 2 records are in 2019 and 1 record is in 2020. So I need retrieve only 2020 record

For the given table output must be (10, 2, 3, 4, 5, 7, 8)

TIA!

>Solution :

The below query returns the customer_website , customer_name from table which is the most recent year.

SELECT customer_website, customer_name
FROM table
WHERE (customer_website, YEAR(date)) IN (
  SELECT customer_website, MAX(YEAR(created_date))
  FROM your_table
  GROUP BY customer_website
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading