PostgreSQL - Removing NULLS row and column from conditional aggregation results
up vote
0
down vote
favorite
I have a query for a multidimensional table using conditional aggregation
select A,
SUM(case when D = 3 then D end) as SUM_D1,
SUM(case when D = 4 then D end) as SUM_D2)
The result:
A SUM_D1 SUM_D2
-------------------
a1 100 NULL
a1 200 NULL
a3 NULL NULL
a4 NULL NULL
However, I would like to hide all NULL rows and columns as follows:
A SUM_D1
-----------
a1 100
a1 200
I have looked for similar problems but they are not my expected answer.
Any help is much appreciated,
Thank you
sql postgresql aggregate-functions
add a comment |
up vote
0
down vote
favorite
I have a query for a multidimensional table using conditional aggregation
select A,
SUM(case when D = 3 then D end) as SUM_D1,
SUM(case when D = 4 then D end) as SUM_D2)
The result:
A SUM_D1 SUM_D2
-------------------
a1 100 NULL
a1 200 NULL
a3 NULL NULL
a4 NULL NULL
However, I would like to hide all NULL rows and columns as follows:
A SUM_D1
-----------
a1 100
a1 200
I have looked for similar problems but they are not my expected answer.
Any help is much appreciated,
Thank you
sql postgresql aggregate-functions
1
Usehaving
to filter forIS NOT NULL
orwhere
plus Derived Table. For not showing columns there's no way using plain SQL.
– dnoeth
yesterday
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a query for a multidimensional table using conditional aggregation
select A,
SUM(case when D = 3 then D end) as SUM_D1,
SUM(case when D = 4 then D end) as SUM_D2)
The result:
A SUM_D1 SUM_D2
-------------------
a1 100 NULL
a1 200 NULL
a3 NULL NULL
a4 NULL NULL
However, I would like to hide all NULL rows and columns as follows:
A SUM_D1
-----------
a1 100
a1 200
I have looked for similar problems but they are not my expected answer.
Any help is much appreciated,
Thank you
sql postgresql aggregate-functions
I have a query for a multidimensional table using conditional aggregation
select A,
SUM(case when D = 3 then D end) as SUM_D1,
SUM(case when D = 4 then D end) as SUM_D2)
The result:
A SUM_D1 SUM_D2
-------------------
a1 100 NULL
a1 200 NULL
a3 NULL NULL
a4 NULL NULL
However, I would like to hide all NULL rows and columns as follows:
A SUM_D1
-----------
a1 100
a1 200
I have looked for similar problems but they are not my expected answer.
Any help is much appreciated,
Thank you
sql postgresql aggregate-functions
sql postgresql aggregate-functions
asked yesterday
Duy Huynh
15310
15310
1
Usehaving
to filter forIS NOT NULL
orwhere
plus Derived Table. For not showing columns there's no way using plain SQL.
– dnoeth
yesterday
add a comment |
1
Usehaving
to filter forIS NOT NULL
orwhere
plus Derived Table. For not showing columns there's no way using plain SQL.
– dnoeth
yesterday
1
1
Use
having
to filter for IS NOT NULL
or where
plus Derived Table. For not showing columns there's no way using plain SQL.– dnoeth
yesterday
Use
having
to filter for IS NOT NULL
or where
plus Derived Table. For not showing columns there's no way using plain SQL.– dnoeth
yesterday
add a comment |
4 Answers
4
active
oldest
votes
up vote
1
down vote
I may have misinterpreted your question because the solution seems so simple:
select A,
SUM(case when D = 3 then D end) as SUM_D1,
SUM(case when D = 4 then D end) as SUM_D2)
where D is not null
This is not what you want, is it? :-)
add a comment |
up vote
0
down vote
The reason for the NULL is when you are grouping for A with a condition of D having 2 when there is no D value as 2 for a value in column A such as a3, then it will be displayed NULL, so you can avoid that by having a derived table, hope this helps,
select A,coalesce(SUM_D1,' ') as SUM_D1 ,coalesce(SUM_D2,' ') as SUM_D2
from
(select A,
SUM(case when D = 3 then D end) as SUM_D1,
SUM(case when D = 4 then D end) as SUM_D2
From table
group by A)B
where SUM_D1 is not null or SUM_D2 is not null
WhenD = 3
it's never NULL, thus coalesce is useless (and it's using a wrong datatype, causing a typcast from numeric to string which is then casted back to numeric for the sum). And the OP doesn't want to disply zero instead of NULL, he wants to suppress those rows/columns
– dnoeth
yesterday
@dnoeth Good point, just wondering if D is 3 how sum of D's can be 100
– Ajan Balakumaran
yesterday
It's probably not a real result :-)
– dnoeth
yesterday
The reason for the null is when a value of A doesn't have any D value as 3 it will be null
– Ajan Balakumaran
yesterday
add a comment |
up vote
0
down vote
Null appear because the condition that's not handled by case statement
select A,
SUM(case when D = 3 then D end) as SUM_D1,
SUM(case when D = 4 then D end) as SUM_D2
from
Table1
group by
A
having
(case when D = 3 or D = 4 then D end) is not null
As comment said if you want to suppress the null value.. You can use having to suppress null using is not null
1
The OP doesn't want to disply zero instead of NULL, he wants to suppress those rows/columns
– dnoeth
yesterday
when d is 3 or 4 it is not null right ?
– Ajan Balakumaran
yesterday
Yes.. D have that both value and i miss the op want.. Op want to suppress those.. Not change value..
– dwir182
yesterday
having D is not null
will not run, after aggregation there's no more individual D, must be in WHERE
– dnoeth
23 hours ago
add a comment |
up vote
0
down vote
I think this does what you want:
select A,
coalesce(sum(case when D = 3 then D end),
sum(case when D = 4 then D end)
) as sum_d
from t
group by A
having sum(case when d in (3, 4) then 1 else 0 end) > 0;
Note that this returns only one column -- as in your example. If both "3" and "4" are in the data, then the value is for the "3"s.
If you want a query that returns a variable number of columns, then you need to use dynamic SQL -- or some other method. SQL queries return a fixed number of columns.
One method would be to return the values as an array:
select a,
array_agg(d order by d) as ds,
array_agg(sumd order by d) as sumds
from (select a, d, sum(d) as sumd
from t
where d in (3, 4)
group by a, d
) d
group by a;
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
I may have misinterpreted your question because the solution seems so simple:
select A,
SUM(case when D = 3 then D end) as SUM_D1,
SUM(case when D = 4 then D end) as SUM_D2)
where D is not null
This is not what you want, is it? :-)
add a comment |
up vote
1
down vote
I may have misinterpreted your question because the solution seems so simple:
select A,
SUM(case when D = 3 then D end) as SUM_D1,
SUM(case when D = 4 then D end) as SUM_D2)
where D is not null
This is not what you want, is it? :-)
add a comment |
up vote
1
down vote
up vote
1
down vote
I may have misinterpreted your question because the solution seems so simple:
select A,
SUM(case when D = 3 then D end) as SUM_D1,
SUM(case when D = 4 then D end) as SUM_D2)
where D is not null
This is not what you want, is it? :-)
I may have misinterpreted your question because the solution seems so simple:
select A,
SUM(case when D = 3 then D end) as SUM_D1,
SUM(case when D = 4 then D end) as SUM_D2)
where D is not null
This is not what you want, is it? :-)
answered yesterday
johey
1009
1009
add a comment |
add a comment |
up vote
0
down vote
The reason for the NULL is when you are grouping for A with a condition of D having 2 when there is no D value as 2 for a value in column A such as a3, then it will be displayed NULL, so you can avoid that by having a derived table, hope this helps,
select A,coalesce(SUM_D1,' ') as SUM_D1 ,coalesce(SUM_D2,' ') as SUM_D2
from
(select A,
SUM(case when D = 3 then D end) as SUM_D1,
SUM(case when D = 4 then D end) as SUM_D2
From table
group by A)B
where SUM_D1 is not null or SUM_D2 is not null
WhenD = 3
it's never NULL, thus coalesce is useless (and it's using a wrong datatype, causing a typcast from numeric to string which is then casted back to numeric for the sum). And the OP doesn't want to disply zero instead of NULL, he wants to suppress those rows/columns
– dnoeth
yesterday
@dnoeth Good point, just wondering if D is 3 how sum of D's can be 100
– Ajan Balakumaran
yesterday
It's probably not a real result :-)
– dnoeth
yesterday
The reason for the null is when a value of A doesn't have any D value as 3 it will be null
– Ajan Balakumaran
yesterday
add a comment |
up vote
0
down vote
The reason for the NULL is when you are grouping for A with a condition of D having 2 when there is no D value as 2 for a value in column A such as a3, then it will be displayed NULL, so you can avoid that by having a derived table, hope this helps,
select A,coalesce(SUM_D1,' ') as SUM_D1 ,coalesce(SUM_D2,' ') as SUM_D2
from
(select A,
SUM(case when D = 3 then D end) as SUM_D1,
SUM(case when D = 4 then D end) as SUM_D2
From table
group by A)B
where SUM_D1 is not null or SUM_D2 is not null
WhenD = 3
it's never NULL, thus coalesce is useless (and it's using a wrong datatype, causing a typcast from numeric to string which is then casted back to numeric for the sum). And the OP doesn't want to disply zero instead of NULL, he wants to suppress those rows/columns
– dnoeth
yesterday
@dnoeth Good point, just wondering if D is 3 how sum of D's can be 100
– Ajan Balakumaran
yesterday
It's probably not a real result :-)
– dnoeth
yesterday
The reason for the null is when a value of A doesn't have any D value as 3 it will be null
– Ajan Balakumaran
yesterday
add a comment |
up vote
0
down vote
up vote
0
down vote
The reason for the NULL is when you are grouping for A with a condition of D having 2 when there is no D value as 2 for a value in column A such as a3, then it will be displayed NULL, so you can avoid that by having a derived table, hope this helps,
select A,coalesce(SUM_D1,' ') as SUM_D1 ,coalesce(SUM_D2,' ') as SUM_D2
from
(select A,
SUM(case when D = 3 then D end) as SUM_D1,
SUM(case when D = 4 then D end) as SUM_D2
From table
group by A)B
where SUM_D1 is not null or SUM_D2 is not null
The reason for the NULL is when you are grouping for A with a condition of D having 2 when there is no D value as 2 for a value in column A such as a3, then it will be displayed NULL, so you can avoid that by having a derived table, hope this helps,
select A,coalesce(SUM_D1,' ') as SUM_D1 ,coalesce(SUM_D2,' ') as SUM_D2
from
(select A,
SUM(case when D = 3 then D end) as SUM_D1,
SUM(case when D = 4 then D end) as SUM_D2
From table
group by A)B
where SUM_D1 is not null or SUM_D2 is not null
edited 23 hours ago
answered yesterday
Ajan Balakumaran
45219
45219
WhenD = 3
it's never NULL, thus coalesce is useless (and it's using a wrong datatype, causing a typcast from numeric to string which is then casted back to numeric for the sum). And the OP doesn't want to disply zero instead of NULL, he wants to suppress those rows/columns
– dnoeth
yesterday
@dnoeth Good point, just wondering if D is 3 how sum of D's can be 100
– Ajan Balakumaran
yesterday
It's probably not a real result :-)
– dnoeth
yesterday
The reason for the null is when a value of A doesn't have any D value as 3 it will be null
– Ajan Balakumaran
yesterday
add a comment |
WhenD = 3
it's never NULL, thus coalesce is useless (and it's using a wrong datatype, causing a typcast from numeric to string which is then casted back to numeric for the sum). And the OP doesn't want to disply zero instead of NULL, he wants to suppress those rows/columns
– dnoeth
yesterday
@dnoeth Good point, just wondering if D is 3 how sum of D's can be 100
– Ajan Balakumaran
yesterday
It's probably not a real result :-)
– dnoeth
yesterday
The reason for the null is when a value of A doesn't have any D value as 3 it will be null
– Ajan Balakumaran
yesterday
When
D = 3
it's never NULL, thus coalesce is useless (and it's using a wrong datatype, causing a typcast from numeric to string which is then casted back to numeric for the sum). And the OP doesn't want to disply zero instead of NULL, he wants to suppress those rows/columns– dnoeth
yesterday
When
D = 3
it's never NULL, thus coalesce is useless (and it's using a wrong datatype, causing a typcast from numeric to string which is then casted back to numeric for the sum). And the OP doesn't want to disply zero instead of NULL, he wants to suppress those rows/columns– dnoeth
yesterday
@dnoeth Good point, just wondering if D is 3 how sum of D's can be 100
– Ajan Balakumaran
yesterday
@dnoeth Good point, just wondering if D is 3 how sum of D's can be 100
– Ajan Balakumaran
yesterday
It's probably not a real result :-)
– dnoeth
yesterday
It's probably not a real result :-)
– dnoeth
yesterday
The reason for the null is when a value of A doesn't have any D value as 3 it will be null
– Ajan Balakumaran
yesterday
The reason for the null is when a value of A doesn't have any D value as 3 it will be null
– Ajan Balakumaran
yesterday
add a comment |
up vote
0
down vote
Null appear because the condition that's not handled by case statement
select A,
SUM(case when D = 3 then D end) as SUM_D1,
SUM(case when D = 4 then D end) as SUM_D2
from
Table1
group by
A
having
(case when D = 3 or D = 4 then D end) is not null
As comment said if you want to suppress the null value.. You can use having to suppress null using is not null
1
The OP doesn't want to disply zero instead of NULL, he wants to suppress those rows/columns
– dnoeth
yesterday
when d is 3 or 4 it is not null right ?
– Ajan Balakumaran
yesterday
Yes.. D have that both value and i miss the op want.. Op want to suppress those.. Not change value..
– dwir182
yesterday
having D is not null
will not run, after aggregation there's no more individual D, must be in WHERE
– dnoeth
23 hours ago
add a comment |
up vote
0
down vote
Null appear because the condition that's not handled by case statement
select A,
SUM(case when D = 3 then D end) as SUM_D1,
SUM(case when D = 4 then D end) as SUM_D2
from
Table1
group by
A
having
(case when D = 3 or D = 4 then D end) is not null
As comment said if you want to suppress the null value.. You can use having to suppress null using is not null
1
The OP doesn't want to disply zero instead of NULL, he wants to suppress those rows/columns
– dnoeth
yesterday
when d is 3 or 4 it is not null right ?
– Ajan Balakumaran
yesterday
Yes.. D have that both value and i miss the op want.. Op want to suppress those.. Not change value..
– dwir182
yesterday
having D is not null
will not run, after aggregation there's no more individual D, must be in WHERE
– dnoeth
23 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
Null appear because the condition that's not handled by case statement
select A,
SUM(case when D = 3 then D end) as SUM_D1,
SUM(case when D = 4 then D end) as SUM_D2
from
Table1
group by
A
having
(case when D = 3 or D = 4 then D end) is not null
As comment said if you want to suppress the null value.. You can use having to suppress null using is not null
Null appear because the condition that's not handled by case statement
select A,
SUM(case when D = 3 then D end) as SUM_D1,
SUM(case when D = 4 then D end) as SUM_D2
from
Table1
group by
A
having
(case when D = 3 or D = 4 then D end) is not null
As comment said if you want to suppress the null value.. You can use having to suppress null using is not null
edited 22 hours ago
answered yesterday
dwir182
1,482318
1,482318
1
The OP doesn't want to disply zero instead of NULL, he wants to suppress those rows/columns
– dnoeth
yesterday
when d is 3 or 4 it is not null right ?
– Ajan Balakumaran
yesterday
Yes.. D have that both value and i miss the op want.. Op want to suppress those.. Not change value..
– dwir182
yesterday
having D is not null
will not run, after aggregation there's no more individual D, must be in WHERE
– dnoeth
23 hours ago
add a comment |
1
The OP doesn't want to disply zero instead of NULL, he wants to suppress those rows/columns
– dnoeth
yesterday
when d is 3 or 4 it is not null right ?
– Ajan Balakumaran
yesterday
Yes.. D have that both value and i miss the op want.. Op want to suppress those.. Not change value..
– dwir182
yesterday
having D is not null
will not run, after aggregation there's no more individual D, must be in WHERE
– dnoeth
23 hours ago
1
1
The OP doesn't want to disply zero instead of NULL, he wants to suppress those rows/columns
– dnoeth
yesterday
The OP doesn't want to disply zero instead of NULL, he wants to suppress those rows/columns
– dnoeth
yesterday
when d is 3 or 4 it is not null right ?
– Ajan Balakumaran
yesterday
when d is 3 or 4 it is not null right ?
– Ajan Balakumaran
yesterday
Yes.. D have that both value and i miss the op want.. Op want to suppress those.. Not change value..
– dwir182
yesterday
Yes.. D have that both value and i miss the op want.. Op want to suppress those.. Not change value..
– dwir182
yesterday
having D is not null
will not run, after aggregation there's no more individual D, must be in WHERE– dnoeth
23 hours ago
having D is not null
will not run, after aggregation there's no more individual D, must be in WHERE– dnoeth
23 hours ago
add a comment |
up vote
0
down vote
I think this does what you want:
select A,
coalesce(sum(case when D = 3 then D end),
sum(case when D = 4 then D end)
) as sum_d
from t
group by A
having sum(case when d in (3, 4) then 1 else 0 end) > 0;
Note that this returns only one column -- as in your example. If both "3" and "4" are in the data, then the value is for the "3"s.
If you want a query that returns a variable number of columns, then you need to use dynamic SQL -- or some other method. SQL queries return a fixed number of columns.
One method would be to return the values as an array:
select a,
array_agg(d order by d) as ds,
array_agg(sumd order by d) as sumds
from (select a, d, sum(d) as sumd
from t
where d in (3, 4)
group by a, d
) d
group by a;
add a comment |
up vote
0
down vote
I think this does what you want:
select A,
coalesce(sum(case when D = 3 then D end),
sum(case when D = 4 then D end)
) as sum_d
from t
group by A
having sum(case when d in (3, 4) then 1 else 0 end) > 0;
Note that this returns only one column -- as in your example. If both "3" and "4" are in the data, then the value is for the "3"s.
If you want a query that returns a variable number of columns, then you need to use dynamic SQL -- or some other method. SQL queries return a fixed number of columns.
One method would be to return the values as an array:
select a,
array_agg(d order by d) as ds,
array_agg(sumd order by d) as sumds
from (select a, d, sum(d) as sumd
from t
where d in (3, 4)
group by a, d
) d
group by a;
add a comment |
up vote
0
down vote
up vote
0
down vote
I think this does what you want:
select A,
coalesce(sum(case when D = 3 then D end),
sum(case when D = 4 then D end)
) as sum_d
from t
group by A
having sum(case when d in (3, 4) then 1 else 0 end) > 0;
Note that this returns only one column -- as in your example. If both "3" and "4" are in the data, then the value is for the "3"s.
If you want a query that returns a variable number of columns, then you need to use dynamic SQL -- or some other method. SQL queries return a fixed number of columns.
One method would be to return the values as an array:
select a,
array_agg(d order by d) as ds,
array_agg(sumd order by d) as sumds
from (select a, d, sum(d) as sumd
from t
where d in (3, 4)
group by a, d
) d
group by a;
I think this does what you want:
select A,
coalesce(sum(case when D = 3 then D end),
sum(case when D = 4 then D end)
) as sum_d
from t
group by A
having sum(case when d in (3, 4) then 1 else 0 end) > 0;
Note that this returns only one column -- as in your example. If both "3" and "4" are in the data, then the value is for the "3"s.
If you want a query that returns a variable number of columns, then you need to use dynamic SQL -- or some other method. SQL queries return a fixed number of columns.
One method would be to return the values as an array:
select a,
array_agg(d order by d) as ds,
array_agg(sumd order by d) as sumds
from (select a, d, sum(d) as sumd
from t
where d in (3, 4)
group by a, d
) d
group by a;
answered 21 hours ago
Gordon Linoff
740k32285389
740k32285389
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53222163%2fpostgresql-removing-nulls-row-and-column-from-conditional-aggregation-results%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Use
having
to filter forIS NOT NULL
orwhere
plus Derived Table. For not showing columns there's no way using plain SQL.– dnoeth
yesterday