Count number of orders over date

Count number of orders over date



I have a table like this: orders(id, created_ts, status)
I want to generate a MySQL query which shows the number of open orders with created_ts.
created_ts is MySQL timestamp



Sample data:


id created_ts status
--- ---------- -------
1 11-1-2017 Open
2 11-1-2017 Open
3 12-1-2017 Open
4 13-1-2017 Open
5 13-1-2017 Closed
6 14-1-2017 Closed



Outpur:


created_ts count
-------- ------
11-1-2017 2
12-1-2017 3
13-1-2017 4
14-1-2017 4



Where count is the number of orders opened for that date.

It is calculated as follows.

count of orders for a date = all orders with status = 'Open' with created_ts <= created_ts of that row.



I don't know how to approach the problem.
could anyone please help me.






What do you mean by created_ts <= created_ts ?

– Noor A Shuvo
Sep 11 '18 at 4:39


created_ts <= created_ts






@NoorAShuvo I meant all orders with status = 'Open' which are created previously to the current processing row.

– Harikrishnan
Sep 11 '18 at 4:41






Then, GROUP BY date(created_ts) will output the result.

– Noor A Shuvo
Sep 11 '18 at 4:43






@NoorAShuvo No. Because the created date will not be the same. I updated the question with sample data

– Harikrishnan
Sep 11 '18 at 4:47




3 Answers
3



It seems you need cumulative sum:


SELECT t.created_ts,
(@running_total := @running_total + count(case when status='Open'then t.id else null end)) AS countval
FROM TABLE t
JOIN (SELECT @running_total := 0) r
group by t.created_ts
ORDER BY t.created_ts






This query won't return the results as I mentioned in the question. The created_ts can be different and it should not be grouped.

– Harikrishnan
Sep 11 '18 at 4:43






can you please give some table structure in your question

– fa06
Sep 11 '18 at 4:44






Sure. I updated the question with sample data

– Harikrishnan
Sep 11 '18 at 4:47






I will try this

– Harikrishnan
Sep 11 '18 at 5:50






It worked! Thank you for your time.

– Harikrishnan
Sep 11 '18 at 10:09



You could use a correlated query to get running total of opened orders for distinct dates


select t1.created_ts,
(select count(*)
from demo
where created_ts <=t1.created_ts
and status = 'Open') date_wise_sum
from (
select distinct created_ts
from demo
) t1



Demo






Thank you for the answer.

– Harikrishnan
Sep 11 '18 at 10:10



If I understand correctly, we look at open records only and ignore the closed records. You want a running total, which you get with a windows function, available as of MySQL 8:


select created_ts, sum(sum(status = 'Open')) over (order by created_ts) as cnt
from mytable
group by created_ts
order by created_ts;



In earlier versions you can for instance join the counts to the dates:


select dates.created_ts, sum(counted.cnt) as total
from
(
select distinct created_ts
from mytable
) dates
join
(
select created_ts, count(*) as cnt
from mytable
where status = 'Open'
group by created_ts
) counted on counted.created_ts <= dates.created_ts
group by dates.created_ts
order by dates.created_ts;






Thank you for the answer.

– Harikrishnan
Sep 11 '18 at 10:10



Thanks for contributing an answer to Stack Overflow!



But avoid



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



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế