简体   繁体   中英

Order a postgreSQL query on a rolling period

it will be easier if i illustrate my question with a concrete example.

my query (postgresql) :

SELECT 
    type.name_type as type_name,
    extract(day from event.creation_date) as days,
    count(0) as number
FROM event, type, event_type
WHERE event.id = event_type.event_id
    AND type.type_id = event_type.type_id
GROUP BY days, type_name;

i would like to order the result according to a rolling month. let me explain :

-> let's say we are currently the 6th of whatever month

-> i want the table returned to be ordered on a month according to this day, which means from the 7th to the 6th (current day)

anyone has an idea of how I could do that ?

any help is appreciated :) have a nice day!

(PS : if these explications aren't enough, i'll try my best to be more specific)

If I understand correctly , you want to order by date :

SELECT 
    type.name_type as type_name,
    extract(day from event.creation_date) as days,
    count(0) as number
FROM event, type, event_type
WHERE event.id = event_type.event_id
    AND type.type_id = event_type.type_id
GROUP BY days, type_name
order by min(event.creation_date) desc

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