From: | Rob Sargentg <robjsargent(at)gmail(dot)com> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Sum of columns |
Date: | 2013-09-09 02:27:50 |
Message-ID: | 522D3226.2030407@gmail.com |
Views: | Whole Thread | Raw Message | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On 09/08/2013 07:12 PM, janek12(at)web(dot)de wrote:
> Hi,
> this is my query:
> SELECT user,
> sum(CASE WHEN lev >= 50 AND lev < 70 THEN 1 ELSE 0 END) as a,
> sum(CASE WHEN lev >= 70 AND lev < 80 THEN 1 ELSE 0 END) as b,
> sum(CASE WHEN lev >= 80 AND lev <= 90 THEN 1 ELSE 0 END) as c,
> sum(CASE WHEN lev > 90 THEN 1 ELSE 0 END) as d,
> (SELECT a + b + a + d) AS matches
> FROM t_temp_fts
> GROUP BY user'
> I like to add up the 4 columns a,b,c and d of every user, but it
> doesn't work like this.
> Does anyone know a solution
> Janek Sendrowski
How far does this get you?
insert into t_temp_fts values('rob', 51), ('rob', 71), ('rob', 81),
('rob', 91);
insert into t_temp_fts values('jon', 51), ('jon', 71), ('jon', 81),
('jon', 91);
SELECT distinct usern,
(select count(*) from t_temp_fts i where o.usern = i.usern and
lev >= 50 AND lev < 70) as a,
(select count(*) from t_temp_fts i where o.usern = i.usern and
lev >= 70 AND lev < 80)as b ,
(select count(*) from t_temp_fts i where o.usern = i.usern and
lev >= 80 AND lev < 90)as c ,
(select count(*) from t_temp_fts i where o.usern = i.usern and
lev > 90) as d
from t_temp_fts o
;
usern | a | b | c | d
-------+---+---+---+---
jon | 1 | 1 | 1 | 1
rob | 1 | 1 | 1 | 1
(2 rows)
From | Date | Subject | |
---|---|---|---|
Next Message | BladeOfLight16 | 2013-09-09 04:10:00 | Re: SQL Path in psql |
Previous Message | Tomas Vondra | 2013-09-09 02:16:33 | Re: Sum of columns |