I want to add week against dates, start day of the date should be Saturday. can anyone tell me how to do it. enter image description here
I tried below code but it didn't solve my purpose
select date_part('week',dates ::date ) as weekly,
date_part('year',dates ::date ) as year,
count(dates)
from finaldata
group by weekly,year
order by weekly,year;
You cannot get what you want form the the standard Postgres date functions
date_part('week', ...)norextract(week from ...). This is because those routines are define with a week in terms of the ISP-8601 Standard (see Documentation ).Your best option is creating a calendar table which defines your particular requirement. Further it is not sufficient just saying the week starts on Saturday. As the ISO definition you must define how the first day of the year is defined. The table and procedure following use define a week as:
Non standard week definition
1. First week of year begins on Jan 1.
2. Subject to #1 above, each week begins on Saturday.
3. Subject to #1 and #2 above, each week contains 7 days.
Then the procedure can be used to generate the calendar for the specified year.
You can then get what you had asked for with:
See demo here.
NOTES:
Since the procedure consists of a only an SQL statement it can be pulled out and run as straight SQL.
The procedure has not been exhaustively tested for various days for first_of_year.