简体   繁体   中英

Group by week and consider Saturday as start day of the week

I want to query my postgresql data and group the results by week, so I'm using the following query:

select
   date_trunc ('week', date_column) as week,
   sum (orders) as orders_count
from database
group by week

But it uses Monday as start day of the week, while I want my weeks to be like 'Saturday -> Friday'. How can I acheive this?

Just subtract two days, and you land on saturday:

select
   date_trunc('week', date_column)::date - 2 as week,
   sum (orders) as orders_count
from the_table
group by week

You can just offset by two days, as follows:

select
   date_trunc ('week', date_column + interval '2 days') - interval '2 days' as week,
   sum (orders) as orders_count
from database
group by week

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM