How to get the records from the last 15 days - Postgresql

Asked

Viewed 2,800 times

3

I have a table called Entrega where all deliveries made with their respective dates are stored, how can I bring only deliveries from the last 15 days? Doing so I can bring deliveries from the day 10 of October, for example:

SELECT * FROM public.entrega where data >= '2017-10-10' order by data desc;

But how can I determine the last 15 days so that it becomes something more dynamic?

2 answers

3


After adapting what was found here my query was like this and I got what I needed:

SELECT * FROM public.entrega where data >= CURRENT_DATE - 15 order by data desc;
  • 1

    Recalling that this query, in addition to returning records from 15 days ago, also returns records from tomorrow until the end of time.

  • @Lacobus, but why does she return the records from tomorrow to infinity? Whenever I run this query she is bringing me the correct result

  • What I mean is, the break is infinite if data >= CURRENT_DATE - 15. If there is any record that shows date future (in relation to current date) in the field data this will also be returned. To recover only últimos 15 dias it would take something like: data >= CURRENT_DATE - 15 AND data <= CURRENT_DATE

  • For example if by chance there is a record with a date 20/10/2017 today in the bank it would be returned in the case? Even using the CURRENT_DATE?

  • That’s right! If you are only applying the condition data >= CURRENT_DATE - 15 dates future will be returned. If today is 16/10/2017 all records of 20/10/2017 would be returned too!

2

Recovers the records contained in the table public.entrega who own the field data completed with dates of the last 15 days, without considering future dates.

SELECT
    *
FROM
    public.entrega
WHERE
    data BETWEEN CURRENT_DATE - '15 days'::interval AND CURRENT_DATE
ORDER BY
    data DESC;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.