1

I want to get last month records from table. I have tried:

SELECT count(*) as numberOfRows from Table where created_at >  CURRENT_DATE - INTERVAL '1 months'

It's Ok, but I want to add some conditions:

If numberOfRows >= 10, do nothing (numberOfRows can be 20, 30, ...)

else if numberOfRows < 10, select from this table until numberOfRows = 10 (last 2 months, 3 months, etc...).

How can I do that? Thanks in advances!

2
  • Do you or don't you want records from more than one month ago? This is not clear to me. Commented Aug 15, 2017 at 10:42
  • Your description sounds pretty useless. Why not just return greatest(count(*), 10)? Commented Aug 15, 2017 at 10:43

2 Answers 2

2
WITH curr_month_cnt AS (
    SELECT COUNT(*) AS cnt
    FROM your_table
    WHERE created_at > CURRENT_DATE - INTERVAL '1 months'
)

SELECT *
FROM your_table
WHERE created_at > CURRENT_DATE - INTERVAL '1 months'
UNION ALL
SELECT t.*
FROM
(
    SELECT *
    FROM your_table
    WHERE
        created_at <= CURRENT_DATE - INTERVAL '1 months' AND
        (SELECT cnt FROM curr_month_cnt) < 10
    ORDER BY created_at desc
    LIMIT
        GREATEST(0, 10 - (SELECT cnt FROM curr_month_cnt))
) t

This will return a maximum of 10 records, starting with the most recent month and going backwards. In the event that the latest month have not 10 records, then two and three months old data would be returned, in that order.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, my description do not enough clear. I want to get all records in latest month. If the event in latest month have not 10 records then going backwards until get enough 10 records. Else if the event in latest month has records greater than 10 will get all of them. The number of rows is 20, 30 will ok too.
If curr_month_cnt is greater than 10, an error occurred: " LIMIT must not be negative". How can I resolve it?
@javimuu Use GREATEST() to limit the expression to being no smaller than zero. This should get around this problem.
0

Based on your description, you would seem to want:

select greatest(10, count(*)) as numberOfRows
from Table
where created_at >  CURRENT_DATE - INTERVAL '1 months';

This seems rather surprising. Perhaps you want:

select (case when sum( (CURRENT_DATE - INTERVAL '1 months' ) :: int) >= 10 
             then sum( (CURRENT_DATE - INTERVAL '1 months' ) :: int)
             else least(10, count(*))
        end) as numberOfRows
from Table
where created_at > CURRENT_DATE - INTERVAL '1 months';

1 Comment

Thanks you for answered my question. An error has occurred: ``` ERROR: cannot cast type timestamp without time zone to integer LINE 1: ...e WHEN SUM( (CURRENT_DATE - INTERVAL '1 months' ) :: int) >=... ``` What should I do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.