Search Postgresql Archives

Re: how to use aggregate functions in this case

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Janek Sendrowski wrote
> Hi,
> 
> Thats my code snipped:
>  
> SELECT  v_rec1.user,
>         sum(CASE WHEN v_rec_fts.lev BETWEEN 0 AND 25 THEN 1 ELSE 0 END) as
> "0 to 25",
>         sum(CASE WHEN v_rec_fts.lev BETWEEN 25 AND 50 THEN 1 ELSE 0 END)
> as "25 to 50",
>         sum(CASE WHEN v_rec_fts.lev BETWEEN 50 AND 100 THEN 1 ELSE 0 END)
> as "50 to 100"
>         INTO v_rec2
>         GROUP BY user;
> 
> Now I want to summuarize the "0 to 25" values and the others in the same
> query.
> Somehow like this: count("0 to 25")
> But I want to do it with every single user and I don't know how to do that
> 
> A result should look like this:
> 
> user      percentage    count
> smith     "0 to 25"     5
> smith     "25 to 50"    7
> smith     "50 to 75"    2
> jones     "0 to 25"     11
> jones     "25 to 50"    1
> jones     "50 to 75"    3
> 
> Hope someone who can help me
> 
> Janek Sendrowski
> 
> 
> -- 
> Sent via pgsql-general mailing list (

> pgsql-general@

> )
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general

You have to write three queries (select statements) and then "UNION ALL"
them together.  Each sub-query has a where clause matching the range you
wish to aggregate for.

David J.





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


-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general





[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux