Re: how to use aggregate functions in this case - Mailing list pgsql-general

From David Johnston
Subject Re: how to use aggregate functions in this case
Date
Msg-id 1377525567350-5768573.post@n5.nabble.com
Whole thread Raw
In response to Re: how to use aggregate functions in this case  (BladeOfLight16 <bladeoflight16@gmail.com>)
Responses Re: how to use aggregate functions in this case  ("Janek Sendrowski" <janek12@web.de>)
List pgsql-general
BladeOfLight16 wrote
> Then again, I guess you don't need a nested query.
>
> SELECT  v_rec1.user,
>         CASE WIDTH_BUCKET(v_rec_fts.lev, 0, 100, 4)
>               WHEN 1 THEN '0 to 25'
>               WHEN 2 THEN '25 to 50'
>               WHEN 3 THEN '50 to 75'
>               WHEN 4 THEN '75 to 100'
>               ELSE 'But how?'
>         END CASE AS quarter_percentage
>          COUNT(*) as count,
> FROM v_rec2
> GROUP BY user, quarter_percentage;

This is clean but requires the use of equal intervals.

Another option, though I am unfamiliar with the exact syntax, is to use the
contains operator and an "intrange" (range type, for integer or whatever
actual type is needed).

SELECT CASE WHEN val @> '(0, 25]' -- '(25,50]', etc...

This allows for uneven bucket sizes and avoid the double-inclusive endpoints
problem that results from using BETWEEN.

Requires 9.2

David J.




--
View this message in context:
http://postgresql.1045698.n5.nabble.com/how-to-use-aggregate-functions-in-this-case-tp5768522p5768573.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


pgsql-general by date:

Previous
From: Samrat Revagade
Date:
Subject: Re: effective_io_concurrency with an SSD SAN?
Next
From: Mistina Michal
Date:
Subject: Re: Re: postmaster.pid still exists after pacemaker stopped postgresql - how to remove