Re: Recursive Queries

From: Julien Rouhaud <rjuju123(at)gmail(dot)com>
To: Rob Northcott <Rob(dot)Northcott(at)compilator(dot)com>
Cc: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Recursive Queries
Date: 2020-04-16 13:24:35
Message-ID: CAOBaU_Z-gLa+jxdSnDzGUFzZKS3yDwN0+XG45fn5pRvYjGMRvg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Apr 16, 2020 at 2:49 PM Rob Northcott
<Rob(dot)Northcott(at)compilator(dot)com> wrote:
>
> From: Alex Magnum <magnum11200(at)gmail(dot)com>
>
> What I would like to do is to create a table as shown below that displays the counts per our for the past n dates.
>
>
>
> I can do this with a function but is there an easy way to use recursive queries?
>
> Counts per hour for given date
>
> HR 2020-04-01 2020-04-02 ... 2020-04-10
> 00 38 33 36
> 01 33 26 18
> 02 26 36 17
> 03 36 18 10
> 04 18 17 3
> 05 17 10 3
> 06 10 3 6
> 07 3 3 10
> . 3 6 13
> . 6 10 22
> . 10 13 12
> 22 13 22 9
> 23 22 11 8
>
> Wouldn’t it be easier to do it the other way round, with a column per hour and a row per date? That way the query just needs one sub-query per hour to select just the count for that hour, and group by date.

Note that you wouldn't need subqueries for that, the FILTER clause can
be used and is supported since version 9.4.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Michael Lewis 2020-04-16 13:25:29 Re: Recursive Queries
Previous Message Rob Northcott 2020-04-16 12:48:56 RE: Recursive Queries