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










share|improve this question

















  • 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















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










share|improve this question

















  • 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













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










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









Duy Huynh

15310




15310







  • 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













  • 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








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













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? :-)






share|improve this answer



























    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





    share|improve this answer






















    • 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











    • 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

















    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






    share|improve this answer


















    • 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

















    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;





    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      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






























      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? :-)






      share|improve this answer
























        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? :-)






        share|improve this answer






















          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? :-)






          share|improve this answer












          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? :-)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          johey

          1009




          1009






















              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





              share|improve this answer






















              • 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











              • 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














              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





              share|improve this answer






















              • 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











              • 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












              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





              share|improve this answer














              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






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 23 hours ago

























              answered yesterday









              Ajan Balakumaran

              45219




              45219











              • 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











              • 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










              • @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










              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






              share|improve this answer


















              • 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














              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






              share|improve this answer


















              • 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












              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






              share|improve this answer














              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







              share|improve this answer














              share|improve this answer



              share|improve this answer








              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












              • 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










              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;





              share|improve this answer
























                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;





                share|improve this answer






















                  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;





                  share|improve this answer












                  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;






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 21 hours ago









                  Gordon Linoff

                  740k32285389




                  740k32285389



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      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














































































                      這個網誌中的熱門文章

                      Is there any way to eliminate the singular point to solve this integral by hand or by approximations?

                      Why am i infinitely getting the same tweet with the Twitter Search API?

                      Solve: $(3xy-2ay^2)dx+(x^2-2axy)dy=0$