Home > Blockchain >  Select for max date of the Inserted dates on every month
Select for max date of the Inserted dates on every month

Time:12-26

I have a sales table It includes each sale date in the date format in the sales_day column. I need to find the maximum date for each month from these entered dates. I am using oracle db

CodePudding user response:

You can group by month and use the max aggregation:

select extract(month from sale_day) as month, max(sale_day) maxdate
from sales
group by extract(month from sale_day);

Fiddle

Note that this will get dates from all the years, which might not be what you want. If you want to limit the rows to a certain year:

select extract(month from sale_day) as month, max(sale_day) maxdate
from sales
where sale_day between TO_DATE('2021-01-01','YYYY-MM-DD') and 
TO_DATE('2021-12-31','YYYY-MM-DD')
group by extract(month from sale_day);
  • Related