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

From BladeOfLight16
Subject Re: how to use aggregate functions in this case
Date
Msg-id CA+=1U=VgJ-Mb_cNe6mANCT71ZTkXtUxm4Sr=AiKViHKbhAYN3Q@mail.gmail.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  (David Johnston <polobo@yahoo.com>)
List pgsql-general
On Sun, Aug 25, 2013 at 8:36 PM, BladeOfLight16 <bladeoflight16@gmail.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;

pgsql-general by date:

Previous
From: Merlin Moncure
Date:
Subject: Re: batch insertion
Next
From: 高健
Date:
Subject: Re: What is the relationship between checkpoint and wal