Running Total of a count per day in SQLite












1















I usually write a couple of very simple queries on a redshift database which used in a marketing visualization tool. And I repeat - VERY simple - I am a novice at this.



I am now faced with a new data source (SQLite) and I'm stuck.



I have a table with :




  • order_id

  • order_date


where there are several orders per day. And I am trying to get a running total of the daily count of order ids.



For example the table would contain :



order_id      order_date  
2541 2017-06-05
26777 2017-06-05
123 2017-06-06
8795 2017-06-07


And I'm trying to get to this result :



Day          RunningTotal  
2017-06-05 : 2
2017-06-06 : 3 (the 2 of the previous day + 1 this day)
2017-06-07 : 4


In POSTGRESQL i would use



SELECT  
order_date,
SUM (COUNT(order_id)) OVER (ORDER BY order_date rows between unbounded preceding and current row) as RunningTotal
FROM table
ORDER BY
order_date
GROUP BY
order_date


How do I do this in SQLite?



I've googled - and see many examples of either SUM or COUNT but seldom combined and never to achieve a running total of a count.










share|improve this question





























    1















    I usually write a couple of very simple queries on a redshift database which used in a marketing visualization tool. And I repeat - VERY simple - I am a novice at this.



    I am now faced with a new data source (SQLite) and I'm stuck.



    I have a table with :




    • order_id

    • order_date


    where there are several orders per day. And I am trying to get a running total of the daily count of order ids.



    For example the table would contain :



    order_id      order_date  
    2541 2017-06-05
    26777 2017-06-05
    123 2017-06-06
    8795 2017-06-07


    And I'm trying to get to this result :



    Day          RunningTotal  
    2017-06-05 : 2
    2017-06-06 : 3 (the 2 of the previous day + 1 this day)
    2017-06-07 : 4


    In POSTGRESQL i would use



    SELECT  
    order_date,
    SUM (COUNT(order_id)) OVER (ORDER BY order_date rows between unbounded preceding and current row) as RunningTotal
    FROM table
    ORDER BY
    order_date
    GROUP BY
    order_date


    How do I do this in SQLite?



    I've googled - and see many examples of either SUM or COUNT but seldom combined and never to achieve a running total of a count.










    share|improve this question



























      1












      1








      1


      1






      I usually write a couple of very simple queries on a redshift database which used in a marketing visualization tool. And I repeat - VERY simple - I am a novice at this.



      I am now faced with a new data source (SQLite) and I'm stuck.



      I have a table with :




      • order_id

      • order_date


      where there are several orders per day. And I am trying to get a running total of the daily count of order ids.



      For example the table would contain :



      order_id      order_date  
      2541 2017-06-05
      26777 2017-06-05
      123 2017-06-06
      8795 2017-06-07


      And I'm trying to get to this result :



      Day          RunningTotal  
      2017-06-05 : 2
      2017-06-06 : 3 (the 2 of the previous day + 1 this day)
      2017-06-07 : 4


      In POSTGRESQL i would use



      SELECT  
      order_date,
      SUM (COUNT(order_id)) OVER (ORDER BY order_date rows between unbounded preceding and current row) as RunningTotal
      FROM table
      ORDER BY
      order_date
      GROUP BY
      order_date


      How do I do this in SQLite?



      I've googled - and see many examples of either SUM or COUNT but seldom combined and never to achieve a running total of a count.










      share|improve this question
















      I usually write a couple of very simple queries on a redshift database which used in a marketing visualization tool. And I repeat - VERY simple - I am a novice at this.



      I am now faced with a new data source (SQLite) and I'm stuck.



      I have a table with :




      • order_id

      • order_date


      where there are several orders per day. And I am trying to get a running total of the daily count of order ids.



      For example the table would contain :



      order_id      order_date  
      2541 2017-06-05
      26777 2017-06-05
      123 2017-06-06
      8795 2017-06-07


      And I'm trying to get to this result :



      Day          RunningTotal  
      2017-06-05 : 2
      2017-06-06 : 3 (the 2 of the previous day + 1 this day)
      2017-06-07 : 4


      In POSTGRESQL i would use



      SELECT  
      order_date,
      SUM (COUNT(order_id)) OVER (ORDER BY order_date rows between unbounded preceding and current row) as RunningTotal
      FROM table
      ORDER BY
      order_date
      GROUP BY
      order_date


      How do I do this in SQLite?



      I've googled - and see many examples of either SUM or COUNT but seldom combined and never to achieve a running total of a count.







      sqlite running-totals






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 18 '17 at 23:24









      RDFozz

      9,89231531




      9,89231531










      asked Aug 18 '17 at 22:44









      user132928user132928

      263




      263






















          3 Answers
          3






          active

          oldest

          votes


















          2














          For anyone trying something similar : this is what worked in the end :



          select
          a.ORDER_DATE,
          sum(b.ID_COUNT) RunningTotal
          from
          (select
          ORDER_DATE, Count(DISTINCT(SOFTWARE_ID)) ID_COUNT
          from orders
          group by ORDER_DATE
          order by ORDER_DATE) a,
          (select
          ORDER_DATE, Count(DISTINCT(SOFTWARE_ID)) ID_COUNT
          from orders
          group by ORDER_DATE
          order by ORDER_DATE) b
          where a.ORDER_DATE >= b.ORDER_DATE
          group by a.ORDER_DATE
          order by a.ORDER_DATE;





          share|improve this answer

































            0














            Maybe this will help: create a view that delivers the COUNT() for each day, then use this view (in a query) to calculate the running total (with SUM()):



            create view order_view
            as
            select
            order_date
            , count(order_id) order_count
            from orders
            group by order_date
            order by order_date ;


            This delivers (using your test data):



            select * from order_view
            -- output
            order_date order_count
            2017-06-05 2
            2017-06-06 1
            2017-06-07 1


            Then, use something like ...



            select 
            a.order_date
            , sum(b.order_count) RunningTotal
            from
            order_view a
            , order_view b
            where a.order_date >= b.order_date
            group by a.order_date
            order by a.order_date;

            -- output
            order_date RunningTotal
            2017-06-05 2
            2017-06-06 3
            2017-06-07 4


            See also: dbfiddle.
            Note that "SQLite does not have a storage class set aside for storing dates and/or times." (see https://sqlite.org/datatype3.html)






            share|improve this answer


























            • Thanks! I wasn't quite able to use it exactly as described above because this Query Visualization Tool does not seem to allow View. But it did get me to start thinking about a different approach!

              – user132928
              Aug 21 '17 at 20:29



















            -1














            you can simply do like this



            select order_date, count(order_date) from orders
            group by order_date





            share|improve this answer








            New contributor




            Rishi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.




















              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "182"
              };
              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: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              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%2fdba.stackexchange.com%2fquestions%2f183877%2frunning-total-of-a-count-per-day-in-sqlite%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              For anyone trying something similar : this is what worked in the end :



              select
              a.ORDER_DATE,
              sum(b.ID_COUNT) RunningTotal
              from
              (select
              ORDER_DATE, Count(DISTINCT(SOFTWARE_ID)) ID_COUNT
              from orders
              group by ORDER_DATE
              order by ORDER_DATE) a,
              (select
              ORDER_DATE, Count(DISTINCT(SOFTWARE_ID)) ID_COUNT
              from orders
              group by ORDER_DATE
              order by ORDER_DATE) b
              where a.ORDER_DATE >= b.ORDER_DATE
              group by a.ORDER_DATE
              order by a.ORDER_DATE;





              share|improve this answer






























                2














                For anyone trying something similar : this is what worked in the end :



                select
                a.ORDER_DATE,
                sum(b.ID_COUNT) RunningTotal
                from
                (select
                ORDER_DATE, Count(DISTINCT(SOFTWARE_ID)) ID_COUNT
                from orders
                group by ORDER_DATE
                order by ORDER_DATE) a,
                (select
                ORDER_DATE, Count(DISTINCT(SOFTWARE_ID)) ID_COUNT
                from orders
                group by ORDER_DATE
                order by ORDER_DATE) b
                where a.ORDER_DATE >= b.ORDER_DATE
                group by a.ORDER_DATE
                order by a.ORDER_DATE;





                share|improve this answer




























                  2












                  2








                  2







                  For anyone trying something similar : this is what worked in the end :



                  select
                  a.ORDER_DATE,
                  sum(b.ID_COUNT) RunningTotal
                  from
                  (select
                  ORDER_DATE, Count(DISTINCT(SOFTWARE_ID)) ID_COUNT
                  from orders
                  group by ORDER_DATE
                  order by ORDER_DATE) a,
                  (select
                  ORDER_DATE, Count(DISTINCT(SOFTWARE_ID)) ID_COUNT
                  from orders
                  group by ORDER_DATE
                  order by ORDER_DATE) b
                  where a.ORDER_DATE >= b.ORDER_DATE
                  group by a.ORDER_DATE
                  order by a.ORDER_DATE;





                  share|improve this answer















                  For anyone trying something similar : this is what worked in the end :



                  select
                  a.ORDER_DATE,
                  sum(b.ID_COUNT) RunningTotal
                  from
                  (select
                  ORDER_DATE, Count(DISTINCT(SOFTWARE_ID)) ID_COUNT
                  from orders
                  group by ORDER_DATE
                  order by ORDER_DATE) a,
                  (select
                  ORDER_DATE, Count(DISTINCT(SOFTWARE_ID)) ID_COUNT
                  from orders
                  group by ORDER_DATE
                  order by ORDER_DATE) b
                  where a.ORDER_DATE >= b.ORDER_DATE
                  group by a.ORDER_DATE
                  order by a.ORDER_DATE;






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Aug 22 '17 at 7:22









                  Andriy M

                  16k63373




                  16k63373










                  answered Aug 21 '17 at 20:30









                  user132928user132928

                  263




                  263

























                      0














                      Maybe this will help: create a view that delivers the COUNT() for each day, then use this view (in a query) to calculate the running total (with SUM()):



                      create view order_view
                      as
                      select
                      order_date
                      , count(order_id) order_count
                      from orders
                      group by order_date
                      order by order_date ;


                      This delivers (using your test data):



                      select * from order_view
                      -- output
                      order_date order_count
                      2017-06-05 2
                      2017-06-06 1
                      2017-06-07 1


                      Then, use something like ...



                      select 
                      a.order_date
                      , sum(b.order_count) RunningTotal
                      from
                      order_view a
                      , order_view b
                      where a.order_date >= b.order_date
                      group by a.order_date
                      order by a.order_date;

                      -- output
                      order_date RunningTotal
                      2017-06-05 2
                      2017-06-06 3
                      2017-06-07 4


                      See also: dbfiddle.
                      Note that "SQLite does not have a storage class set aside for storing dates and/or times." (see https://sqlite.org/datatype3.html)






                      share|improve this answer


























                      • Thanks! I wasn't quite able to use it exactly as described above because this Query Visualization Tool does not seem to allow View. But it did get me to start thinking about a different approach!

                        – user132928
                        Aug 21 '17 at 20:29
















                      0














                      Maybe this will help: create a view that delivers the COUNT() for each day, then use this view (in a query) to calculate the running total (with SUM()):



                      create view order_view
                      as
                      select
                      order_date
                      , count(order_id) order_count
                      from orders
                      group by order_date
                      order by order_date ;


                      This delivers (using your test data):



                      select * from order_view
                      -- output
                      order_date order_count
                      2017-06-05 2
                      2017-06-06 1
                      2017-06-07 1


                      Then, use something like ...



                      select 
                      a.order_date
                      , sum(b.order_count) RunningTotal
                      from
                      order_view a
                      , order_view b
                      where a.order_date >= b.order_date
                      group by a.order_date
                      order by a.order_date;

                      -- output
                      order_date RunningTotal
                      2017-06-05 2
                      2017-06-06 3
                      2017-06-07 4


                      See also: dbfiddle.
                      Note that "SQLite does not have a storage class set aside for storing dates and/or times." (see https://sqlite.org/datatype3.html)






                      share|improve this answer


























                      • Thanks! I wasn't quite able to use it exactly as described above because this Query Visualization Tool does not seem to allow View. But it did get me to start thinking about a different approach!

                        – user132928
                        Aug 21 '17 at 20:29














                      0












                      0








                      0







                      Maybe this will help: create a view that delivers the COUNT() for each day, then use this view (in a query) to calculate the running total (with SUM()):



                      create view order_view
                      as
                      select
                      order_date
                      , count(order_id) order_count
                      from orders
                      group by order_date
                      order by order_date ;


                      This delivers (using your test data):



                      select * from order_view
                      -- output
                      order_date order_count
                      2017-06-05 2
                      2017-06-06 1
                      2017-06-07 1


                      Then, use something like ...



                      select 
                      a.order_date
                      , sum(b.order_count) RunningTotal
                      from
                      order_view a
                      , order_view b
                      where a.order_date >= b.order_date
                      group by a.order_date
                      order by a.order_date;

                      -- output
                      order_date RunningTotal
                      2017-06-05 2
                      2017-06-06 3
                      2017-06-07 4


                      See also: dbfiddle.
                      Note that "SQLite does not have a storage class set aside for storing dates and/or times." (see https://sqlite.org/datatype3.html)






                      share|improve this answer















                      Maybe this will help: create a view that delivers the COUNT() for each day, then use this view (in a query) to calculate the running total (with SUM()):



                      create view order_view
                      as
                      select
                      order_date
                      , count(order_id) order_count
                      from orders
                      group by order_date
                      order by order_date ;


                      This delivers (using your test data):



                      select * from order_view
                      -- output
                      order_date order_count
                      2017-06-05 2
                      2017-06-06 1
                      2017-06-07 1


                      Then, use something like ...



                      select 
                      a.order_date
                      , sum(b.order_count) RunningTotal
                      from
                      order_view a
                      , order_view b
                      where a.order_date >= b.order_date
                      group by a.order_date
                      order by a.order_date;

                      -- output
                      order_date RunningTotal
                      2017-06-05 2
                      2017-06-06 3
                      2017-06-07 4


                      See also: dbfiddle.
                      Note that "SQLite does not have a storage class set aside for storing dates and/or times." (see https://sqlite.org/datatype3.html)







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Aug 19 '17 at 8:01

























                      answered Aug 19 '17 at 6:25









                      stefanstefan

                      2,142139




                      2,142139













                      • Thanks! I wasn't quite able to use it exactly as described above because this Query Visualization Tool does not seem to allow View. But it did get me to start thinking about a different approach!

                        – user132928
                        Aug 21 '17 at 20:29



















                      • Thanks! I wasn't quite able to use it exactly as described above because this Query Visualization Tool does not seem to allow View. But it did get me to start thinking about a different approach!

                        – user132928
                        Aug 21 '17 at 20:29

















                      Thanks! I wasn't quite able to use it exactly as described above because this Query Visualization Tool does not seem to allow View. But it did get me to start thinking about a different approach!

                      – user132928
                      Aug 21 '17 at 20:29





                      Thanks! I wasn't quite able to use it exactly as described above because this Query Visualization Tool does not seem to allow View. But it did get me to start thinking about a different approach!

                      – user132928
                      Aug 21 '17 at 20:29











                      -1














                      you can simply do like this



                      select order_date, count(order_date) from orders
                      group by order_date





                      share|improve this answer








                      New contributor




                      Rishi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.

























                        -1














                        you can simply do like this



                        select order_date, count(order_date) from orders
                        group by order_date





                        share|improve this answer








                        New contributor




                        Rishi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.























                          -1












                          -1








                          -1







                          you can simply do like this



                          select order_date, count(order_date) from orders
                          group by order_date





                          share|improve this answer








                          New contributor




                          Rishi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.










                          you can simply do like this



                          select order_date, count(order_date) from orders
                          group by order_date






                          share|improve this answer








                          New contributor




                          Rishi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          share|improve this answer



                          share|improve this answer






                          New contributor




                          Rishi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          answered 11 mins ago









                          RishiRishi

                          1




                          1




                          New contributor




                          Rishi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.





                          New contributor





                          Rishi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






                          Rishi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Database Administrators Stack Exchange!


                              • 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%2fdba.stackexchange.com%2fquestions%2f183877%2frunning-total-of-a-count-per-day-in-sqlite%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







                              Popular posts from this blog

                              Liste der Baudenkmale in Friedland (Mecklenburg)

                              Single-Malt-Whisky

                              Czorneboh