Creating columns by subaggregating by condition in Snowflake SQL










2














I have following table:



id1 | id2 | n_products | daydiff
a | 1 | 12 | 12
a | 1 | 11 | 13
a | 1 | 90 | 46
a | 2 | 5 | 5
b | 2 | 15 | 15
b | 2 | 15 | 21
c | 3 | 90 | 7


I need to aggregate this table by id and using daydiff in the following manner:



  • if daydiff is less than 14

  • if daydiff is between 14 and 28

  • if daydiff is more than 28.

this should be aggregated using mean.



The result should be:



id1 | id2 | sub 14 | 14_28 | 28+
a | 1 | 11.5 | 0 | 46
a | 2 | 5 | 0 | 0
b | 2 | 0 | 15 | 0
a | 3 | 7 | 0 | 0


How can I achieve this? I guess this would involve some group by statements, but I am not sure how should they be applied










share|improve this question




























    2














    I have following table:



    id1 | id2 | n_products | daydiff
    a | 1 | 12 | 12
    a | 1 | 11 | 13
    a | 1 | 90 | 46
    a | 2 | 5 | 5
    b | 2 | 15 | 15
    b | 2 | 15 | 21
    c | 3 | 90 | 7


    I need to aggregate this table by id and using daydiff in the following manner:



    • if daydiff is less than 14

    • if daydiff is between 14 and 28

    • if daydiff is more than 28.

    this should be aggregated using mean.



    The result should be:



    id1 | id2 | sub 14 | 14_28 | 28+
    a | 1 | 11.5 | 0 | 46
    a | 2 | 5 | 0 | 0
    b | 2 | 0 | 15 | 0
    a | 3 | 7 | 0 | 0


    How can I achieve this? I guess this would involve some group by statements, but I am not sure how should they be applied










    share|improve this question


























      2












      2








      2







      I have following table:



      id1 | id2 | n_products | daydiff
      a | 1 | 12 | 12
      a | 1 | 11 | 13
      a | 1 | 90 | 46
      a | 2 | 5 | 5
      b | 2 | 15 | 15
      b | 2 | 15 | 21
      c | 3 | 90 | 7


      I need to aggregate this table by id and using daydiff in the following manner:



      • if daydiff is less than 14

      • if daydiff is between 14 and 28

      • if daydiff is more than 28.

      this should be aggregated using mean.



      The result should be:



      id1 | id2 | sub 14 | 14_28 | 28+
      a | 1 | 11.5 | 0 | 46
      a | 2 | 5 | 0 | 0
      b | 2 | 0 | 15 | 0
      a | 3 | 7 | 0 | 0


      How can I achieve this? I guess this would involve some group by statements, but I am not sure how should they be applied










      share|improve this question















      I have following table:



      id1 | id2 | n_products | daydiff
      a | 1 | 12 | 12
      a | 1 | 11 | 13
      a | 1 | 90 | 46
      a | 2 | 5 | 5
      b | 2 | 15 | 15
      b | 2 | 15 | 21
      c | 3 | 90 | 7


      I need to aggregate this table by id and using daydiff in the following manner:



      • if daydiff is less than 14

      • if daydiff is between 14 and 28

      • if daydiff is more than 28.

      this should be aggregated using mean.



      The result should be:



      id1 | id2 | sub 14 | 14_28 | 28+
      a | 1 | 11.5 | 0 | 46
      a | 2 | 5 | 0 | 0
      b | 2 | 0 | 15 | 0
      a | 3 | 7 | 0 | 0


      How can I achieve this? I guess this would involve some group by statements, but I am not sure how should they be applied







      sql group-by snowflake-datawarehouse






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 17 at 6:45









      Marcin Zukowski

      1,8251819




      1,8251819










      asked Oct 12 at 12:07









      johnnyheineken

      156114




      156114






















          2 Answers
          2






          active

          oldest

          votes


















          1














          Gordon's answer is cross platform correct, but for myself I prefer the snowflake IFF syntax



          SELECT id1, id2,
          AVG(IFF(datediff < 14, n_products, NULL)) as avg_lt14,
          AVG(IFF(datediff >= 14 and datediff <= 28, n_products, NULL)) as avg_14_28,
          AVG(IFF(datediff > 29, n_products, NULL)) as avg_29pl
          FROM t
          GROUP BY id1, id2;





          share|improve this answer




















          • @johnnyheinekin . . . Do you really prefer non-standard functions such as IIF() to standard SQL constructs such as CASE?
            – Gordon Linoff
            Nov 12 at 11:28


















          3














          Use conditional aggregation:



          select id1, id2,
          avg(case when datediff < 14 then n_products end) as avg_lt14,
          avg(case when datediff >= 14 and datediff <= 28 then n_products end) as avg_14_28,
          avg(case when datediff > 29 then n_products end) as avg_29pl
          from t
          group by id1, id2;


          Some databases calculate the averages of integers as an integer. I don't know if Snowflake does this. If so, then change n_products to n_products * 1.0.






          share|improve this answer




















          • snowflake returns 3dp floats
            – Simeon Pilgrim
            Nov 12 at 3:53










          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',
          autoActivateHeartbeat: false,
          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%2f52779217%2fcreating-columns-by-subaggregating-by-condition-in-snowflake-sql%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Gordon's answer is cross platform correct, but for myself I prefer the snowflake IFF syntax



          SELECT id1, id2,
          AVG(IFF(datediff < 14, n_products, NULL)) as avg_lt14,
          AVG(IFF(datediff >= 14 and datediff <= 28, n_products, NULL)) as avg_14_28,
          AVG(IFF(datediff > 29, n_products, NULL)) as avg_29pl
          FROM t
          GROUP BY id1, id2;





          share|improve this answer




















          • @johnnyheinekin . . . Do you really prefer non-standard functions such as IIF() to standard SQL constructs such as CASE?
            – Gordon Linoff
            Nov 12 at 11:28















          1














          Gordon's answer is cross platform correct, but for myself I prefer the snowflake IFF syntax



          SELECT id1, id2,
          AVG(IFF(datediff < 14, n_products, NULL)) as avg_lt14,
          AVG(IFF(datediff >= 14 and datediff <= 28, n_products, NULL)) as avg_14_28,
          AVG(IFF(datediff > 29, n_products, NULL)) as avg_29pl
          FROM t
          GROUP BY id1, id2;





          share|improve this answer




















          • @johnnyheinekin . . . Do you really prefer non-standard functions such as IIF() to standard SQL constructs such as CASE?
            – Gordon Linoff
            Nov 12 at 11:28













          1












          1








          1






          Gordon's answer is cross platform correct, but for myself I prefer the snowflake IFF syntax



          SELECT id1, id2,
          AVG(IFF(datediff < 14, n_products, NULL)) as avg_lt14,
          AVG(IFF(datediff >= 14 and datediff <= 28, n_products, NULL)) as avg_14_28,
          AVG(IFF(datediff > 29, n_products, NULL)) as avg_29pl
          FROM t
          GROUP BY id1, id2;





          share|improve this answer












          Gordon's answer is cross platform correct, but for myself I prefer the snowflake IFF syntax



          SELECT id1, id2,
          AVG(IFF(datediff < 14, n_products, NULL)) as avg_lt14,
          AVG(IFF(datediff >= 14 and datediff <= 28, n_products, NULL)) as avg_14_28,
          AVG(IFF(datediff > 29, n_products, NULL)) as avg_29pl
          FROM t
          GROUP BY id1, id2;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 3:56









          Simeon Pilgrim

          5,1111929




          5,1111929











          • @johnnyheinekin . . . Do you really prefer non-standard functions such as IIF() to standard SQL constructs such as CASE?
            – Gordon Linoff
            Nov 12 at 11:28
















          • @johnnyheinekin . . . Do you really prefer non-standard functions such as IIF() to standard SQL constructs such as CASE?
            – Gordon Linoff
            Nov 12 at 11:28















          @johnnyheinekin . . . Do you really prefer non-standard functions such as IIF() to standard SQL constructs such as CASE?
          – Gordon Linoff
          Nov 12 at 11:28




          @johnnyheinekin . . . Do you really prefer non-standard functions such as IIF() to standard SQL constructs such as CASE?
          – Gordon Linoff
          Nov 12 at 11:28













          3














          Use conditional aggregation:



          select id1, id2,
          avg(case when datediff < 14 then n_products end) as avg_lt14,
          avg(case when datediff >= 14 and datediff <= 28 then n_products end) as avg_14_28,
          avg(case when datediff > 29 then n_products end) as avg_29pl
          from t
          group by id1, id2;


          Some databases calculate the averages of integers as an integer. I don't know if Snowflake does this. If so, then change n_products to n_products * 1.0.






          share|improve this answer




















          • snowflake returns 3dp floats
            – Simeon Pilgrim
            Nov 12 at 3:53















          3














          Use conditional aggregation:



          select id1, id2,
          avg(case when datediff < 14 then n_products end) as avg_lt14,
          avg(case when datediff >= 14 and datediff <= 28 then n_products end) as avg_14_28,
          avg(case when datediff > 29 then n_products end) as avg_29pl
          from t
          group by id1, id2;


          Some databases calculate the averages of integers as an integer. I don't know if Snowflake does this. If so, then change n_products to n_products * 1.0.






          share|improve this answer




















          • snowflake returns 3dp floats
            – Simeon Pilgrim
            Nov 12 at 3:53













          3












          3








          3






          Use conditional aggregation:



          select id1, id2,
          avg(case when datediff < 14 then n_products end) as avg_lt14,
          avg(case when datediff >= 14 and datediff <= 28 then n_products end) as avg_14_28,
          avg(case when datediff > 29 then n_products end) as avg_29pl
          from t
          group by id1, id2;


          Some databases calculate the averages of integers as an integer. I don't know if Snowflake does this. If so, then change n_products to n_products * 1.0.






          share|improve this answer












          Use conditional aggregation:



          select id1, id2,
          avg(case when datediff < 14 then n_products end) as avg_lt14,
          avg(case when datediff >= 14 and datediff <= 28 then n_products end) as avg_14_28,
          avg(case when datediff > 29 then n_products end) as avg_29pl
          from t
          group by id1, id2;


          Some databases calculate the averages of integers as an integer. I don't know if Snowflake does this. If so, then change n_products to n_products * 1.0.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 12 at 12:10









          Gordon Linoff

          755k35290397




          755k35290397











          • snowflake returns 3dp floats
            – Simeon Pilgrim
            Nov 12 at 3:53
















          • snowflake returns 3dp floats
            – Simeon Pilgrim
            Nov 12 at 3:53















          snowflake returns 3dp floats
          – Simeon Pilgrim
          Nov 12 at 3:53




          snowflake returns 3dp floats
          – Simeon Pilgrim
          Nov 12 at 3:53

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52779217%2fcreating-columns-by-subaggregating-by-condition-in-snowflake-sql%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto