Re: how to use aggregate functions in this case

From: BladeOfLight16 <bladeoflight16(at)gmail(dot)com>
To: Janek Sendrowski <janek12(at)web(dot)de>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: how to use aggregate functions in this case
Date: 2013-08-26 00:48:56
Message-ID: CA+=1U=VgJ-Mb_cNe6mANCT71ZTkXtUxm4Sr=AiKViHKbhAYN3Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sun, Aug 25, 2013 at 8:36 PM, BladeOfLight16 <bladeoflight16(at)gmail(dot)com>wrote:

> This appears to be some kind of equal interval problem.
>
> SELECT v_rec1.user,
> WIDTH_BUCKET(v_rec_fts.lev, 0, 100, 4) AS bucket
> COUNT(*) as count,
> FROM v_rec2
> GROUP BY user, bucket;
>
> (Untested, but this should be the gist.)
>
> Bucket 1 would be 0 to 25, bucket 2 is 25 to 50, 3 is 50 to 75, 4 is 75 to
> 100. If you really need to change the bucket number to some kind of text,
> you can probably nest this query inside another that uses a CASE to pick
> the text based on on the bucket number.
>
> Good luck.
>

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;

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Merlin Moncure 2013-08-26 00:49:15 Re: batch insertion
Previous Message BladeOfLight16 2013-08-26 00:36:59 Re: how to use aggregate functions in this case