Running Total of a count per day in SQLite
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
add a comment |
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
add a comment |
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
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
sqlite running-totals
edited Aug 18 '17 at 23:24
RDFozz
9,89231531
9,89231531
asked Aug 18 '17 at 22:44
user132928user132928
263
263
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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;
add a comment |
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)
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
add a comment |
you can simply do like this
select order_date, count(order_date) from orders
group by order_date
New contributor
add a comment |
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
});
}
});
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
Required, but never shown
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
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;
add a comment |
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;
add a comment |
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;
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;
edited Aug 22 '17 at 7:22
Andriy M
16k63373
16k63373
answered Aug 21 '17 at 20:30
user132928user132928
263
263
add a comment |
add a comment |
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
you can simply do like this
select order_date, count(order_date) from orders
group by order_date
New contributor
add a comment |
you can simply do like this
select order_date, count(order_date) from orders
group by order_date
New contributor
add a comment |
you can simply do like this
select order_date, count(order_date) from orders
group by order_date
New contributor
you can simply do like this
select order_date, count(order_date) from orders
group by order_date
New contributor
New contributor
answered 11 mins ago
RishiRishi
1
1
New contributor
New contributor
add a comment |
add a comment |
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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