Count the number of items in a certain status for a given date
I'm trying to count the number of orders in a given status for given date. In example data, the order status is a timeline that is only updated when the order moves on to the next status.
Sample Data:
Order_ID Status Status_Date
-------- ------ -----------------------
56845 NEW 2012-09-11 11:52:20.000
56845 SENT 2012-09-12 00:22:13.000
56845 ACK 2012-09-17 08:14:33.000
56845 FILL 2012-09-20 14:32:00.000
36968 NEW 2012-09-08 11:52:20.000
36968 SENT 2012-09-15 00:22:13.000
36968 ACK 2012-09-22 08:14:33.000
48258 NEW 2012-09-14 11:52:20.000
48258 SENT 2012-09-20 00:22:13.000
48258 ACK 2012-09-22 08:14:33.000
48258 FILL 2012-09-28 23:22:46.000
48258 SHIP 2012-09-29 18:54:22.000
My difficulty is I need to select the last row that was entered for each order that is less than a specific date and count it if it is in a certain status.
So if I'm using the status = 'SENT' and a date of 9/12/2018 at midnight, I want to return order-id 56845 and 36968. If I'm using a status = 'SENT' and a date of 9/21/2018 at midnight, I want to return order-id 36968 and 48258 because order-id 56845 is in 'FILL' status and no longer in 'SENT'.
sql sql-server-2014
add a comment |
I'm trying to count the number of orders in a given status for given date. In example data, the order status is a timeline that is only updated when the order moves on to the next status.
Sample Data:
Order_ID Status Status_Date
-------- ------ -----------------------
56845 NEW 2012-09-11 11:52:20.000
56845 SENT 2012-09-12 00:22:13.000
56845 ACK 2012-09-17 08:14:33.000
56845 FILL 2012-09-20 14:32:00.000
36968 NEW 2012-09-08 11:52:20.000
36968 SENT 2012-09-15 00:22:13.000
36968 ACK 2012-09-22 08:14:33.000
48258 NEW 2012-09-14 11:52:20.000
48258 SENT 2012-09-20 00:22:13.000
48258 ACK 2012-09-22 08:14:33.000
48258 FILL 2012-09-28 23:22:46.000
48258 SHIP 2012-09-29 18:54:22.000
My difficulty is I need to select the last row that was entered for each order that is less than a specific date and count it if it is in a certain status.
So if I'm using the status = 'SENT' and a date of 9/12/2018 at midnight, I want to return order-id 56845 and 36968. If I'm using a status = 'SENT' and a date of 9/21/2018 at midnight, I want to return order-id 36968 and 48258 because order-id 56845 is in 'FILL' status and no longer in 'SENT'.
sql sql-server-2014
what have you tried so far?
– Vamsi Prabhala
Nov 15 '18 at 18:12
Basically I somehow need the last status (top 1) for each order-id where the status date is < than a given date. Its selecting the top 1 for each order-id that is given me trouble
– mlwrig2
Nov 20 '18 at 21:54
add a comment |
I'm trying to count the number of orders in a given status for given date. In example data, the order status is a timeline that is only updated when the order moves on to the next status.
Sample Data:
Order_ID Status Status_Date
-------- ------ -----------------------
56845 NEW 2012-09-11 11:52:20.000
56845 SENT 2012-09-12 00:22:13.000
56845 ACK 2012-09-17 08:14:33.000
56845 FILL 2012-09-20 14:32:00.000
36968 NEW 2012-09-08 11:52:20.000
36968 SENT 2012-09-15 00:22:13.000
36968 ACK 2012-09-22 08:14:33.000
48258 NEW 2012-09-14 11:52:20.000
48258 SENT 2012-09-20 00:22:13.000
48258 ACK 2012-09-22 08:14:33.000
48258 FILL 2012-09-28 23:22:46.000
48258 SHIP 2012-09-29 18:54:22.000
My difficulty is I need to select the last row that was entered for each order that is less than a specific date and count it if it is in a certain status.
So if I'm using the status = 'SENT' and a date of 9/12/2018 at midnight, I want to return order-id 56845 and 36968. If I'm using a status = 'SENT' and a date of 9/21/2018 at midnight, I want to return order-id 36968 and 48258 because order-id 56845 is in 'FILL' status and no longer in 'SENT'.
sql sql-server-2014
I'm trying to count the number of orders in a given status for given date. In example data, the order status is a timeline that is only updated when the order moves on to the next status.
Sample Data:
Order_ID Status Status_Date
-------- ------ -----------------------
56845 NEW 2012-09-11 11:52:20.000
56845 SENT 2012-09-12 00:22:13.000
56845 ACK 2012-09-17 08:14:33.000
56845 FILL 2012-09-20 14:32:00.000
36968 NEW 2012-09-08 11:52:20.000
36968 SENT 2012-09-15 00:22:13.000
36968 ACK 2012-09-22 08:14:33.000
48258 NEW 2012-09-14 11:52:20.000
48258 SENT 2012-09-20 00:22:13.000
48258 ACK 2012-09-22 08:14:33.000
48258 FILL 2012-09-28 23:22:46.000
48258 SHIP 2012-09-29 18:54:22.000
My difficulty is I need to select the last row that was entered for each order that is less than a specific date and count it if it is in a certain status.
So if I'm using the status = 'SENT' and a date of 9/12/2018 at midnight, I want to return order-id 56845 and 36968. If I'm using a status = 'SENT' and a date of 9/21/2018 at midnight, I want to return order-id 36968 and 48258 because order-id 56845 is in 'FILL' status and no longer in 'SENT'.
sql sql-server-2014
sql sql-server-2014
edited Nov 15 '18 at 19:36
mlwrig2
asked Nov 15 '18 at 18:06
mlwrig2mlwrig2
42
42
what have you tried so far?
– Vamsi Prabhala
Nov 15 '18 at 18:12
Basically I somehow need the last status (top 1) for each order-id where the status date is < than a given date. Its selecting the top 1 for each order-id that is given me trouble
– mlwrig2
Nov 20 '18 at 21:54
add a comment |
what have you tried so far?
– Vamsi Prabhala
Nov 15 '18 at 18:12
Basically I somehow need the last status (top 1) for each order-id where the status date is < than a given date. Its selecting the top 1 for each order-id that is given me trouble
– mlwrig2
Nov 20 '18 at 21:54
what have you tried so far?
– Vamsi Prabhala
Nov 15 '18 at 18:12
what have you tried so far?
– Vamsi Prabhala
Nov 15 '18 at 18:12
Basically I somehow need the last status (top 1) for each order-id where the status date is < than a given date. Its selecting the top 1 for each order-id that is given me trouble
– mlwrig2
Nov 20 '18 at 21:54
Basically I somehow need the last status (top 1) for each order-id where the status date is < than a given date. Its selecting the top 1 for each order-id that is given me trouble
– mlwrig2
Nov 20 '18 at 21:54
add a comment |
3 Answers
3
active
oldest
votes
select count(*) from(select order_id,status from table group by order_id having max(status_date) <= '2012-09-16' or max(status_date) = '2012-09-21' and status ='SENT' )
This is what you are looking for this means if at max status date for each individual order if the items status doesnt change or it is sent until max date less than your date you get the correct result.
This is close but it is returning all of the orders that went through sent status. I just need the ones that were in sent status as of that date. So for a date of 9/12/2012 it should return 2 (Order-ID 56845 and 36968) and for a date of 9/21/2012 it still should return 2 (Order-ID 36968 and 48258)
– mlwrig2
Nov 20 '18 at 21:53
can you please check now ive edited it
– Himanshu Ahuja
Nov 20 '18 at 22:01
add a comment |
You can use below one
select count(1) from table_name where status='NEW' group by Status_Date
or you can use this one to get count for all status in all dates
select count(1) as count,Status,left(Status_Date,10) as Status_Date from table_name
group by Status,left(Status_Date,10)
I failed to mention the dates are really a time line that only get updated when the order switches to a new status. So if the date is midnight on 9/20/2018, I only have 2 orders in SENT status as the first order, has moved onto FILL status
– mlwrig2
Nov 15 '18 at 18:44
@mlwrig2 that is an important requirement. Please edit that into your question so that it will be seen
– Hans Kesting
Nov 15 '18 at 18:55
Thanks, can't tell I'ma newbie can you :)
– mlwrig2
Nov 15 '18 at 19:09
add a comment |
If you want such a specific counter:
SELECT COUNT(*) AS counter FROM orders WHERE Status = 'SENT' and Status_Date <= '2012-09-16 23:59:59.000'
or
SELECT COUNT(*) AS counter FROM orders WHERE Status = 'SENT' and Status_Date <= Convert(datetime, '2012-09-16 23:59:59.000')
to convert the string literal to date.
add a comment |
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
);
);
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%2fstackoverflow.com%2fquestions%2f53325480%2fcount-the-number-of-items-in-a-certain-status-for-a-given-date%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
select count(*) from(select order_id,status from table group by order_id having max(status_date) <= '2012-09-16' or max(status_date) = '2012-09-21' and status ='SENT' )
This is what you are looking for this means if at max status date for each individual order if the items status doesnt change or it is sent until max date less than your date you get the correct result.
This is close but it is returning all of the orders that went through sent status. I just need the ones that were in sent status as of that date. So for a date of 9/12/2012 it should return 2 (Order-ID 56845 and 36968) and for a date of 9/21/2012 it still should return 2 (Order-ID 36968 and 48258)
– mlwrig2
Nov 20 '18 at 21:53
can you please check now ive edited it
– Himanshu Ahuja
Nov 20 '18 at 22:01
add a comment |
select count(*) from(select order_id,status from table group by order_id having max(status_date) <= '2012-09-16' or max(status_date) = '2012-09-21' and status ='SENT' )
This is what you are looking for this means if at max status date for each individual order if the items status doesnt change or it is sent until max date less than your date you get the correct result.
This is close but it is returning all of the orders that went through sent status. I just need the ones that were in sent status as of that date. So for a date of 9/12/2012 it should return 2 (Order-ID 56845 and 36968) and for a date of 9/21/2012 it still should return 2 (Order-ID 36968 and 48258)
– mlwrig2
Nov 20 '18 at 21:53
can you please check now ive edited it
– Himanshu Ahuja
Nov 20 '18 at 22:01
add a comment |
select count(*) from(select order_id,status from table group by order_id having max(status_date) <= '2012-09-16' or max(status_date) = '2012-09-21' and status ='SENT' )
This is what you are looking for this means if at max status date for each individual order if the items status doesnt change or it is sent until max date less than your date you get the correct result.
select count(*) from(select order_id,status from table group by order_id having max(status_date) <= '2012-09-16' or max(status_date) = '2012-09-21' and status ='SENT' )
This is what you are looking for this means if at max status date for each individual order if the items status doesnt change or it is sent until max date less than your date you get the correct result.
edited Nov 20 '18 at 22:01
answered Nov 15 '18 at 18:46
Himanshu AhujaHimanshu Ahuja
9682218
9682218
This is close but it is returning all of the orders that went through sent status. I just need the ones that were in sent status as of that date. So for a date of 9/12/2012 it should return 2 (Order-ID 56845 and 36968) and for a date of 9/21/2012 it still should return 2 (Order-ID 36968 and 48258)
– mlwrig2
Nov 20 '18 at 21:53
can you please check now ive edited it
– Himanshu Ahuja
Nov 20 '18 at 22:01
add a comment |
This is close but it is returning all of the orders that went through sent status. I just need the ones that were in sent status as of that date. So for a date of 9/12/2012 it should return 2 (Order-ID 56845 and 36968) and for a date of 9/21/2012 it still should return 2 (Order-ID 36968 and 48258)
– mlwrig2
Nov 20 '18 at 21:53
can you please check now ive edited it
– Himanshu Ahuja
Nov 20 '18 at 22:01
This is close but it is returning all of the orders that went through sent status. I just need the ones that were in sent status as of that date. So for a date of 9/12/2012 it should return 2 (Order-ID 56845 and 36968) and for a date of 9/21/2012 it still should return 2 (Order-ID 36968 and 48258)
– mlwrig2
Nov 20 '18 at 21:53
This is close but it is returning all of the orders that went through sent status. I just need the ones that were in sent status as of that date. So for a date of 9/12/2012 it should return 2 (Order-ID 56845 and 36968) and for a date of 9/21/2012 it still should return 2 (Order-ID 36968 and 48258)
– mlwrig2
Nov 20 '18 at 21:53
can you please check now ive edited it
– Himanshu Ahuja
Nov 20 '18 at 22:01
can you please check now ive edited it
– Himanshu Ahuja
Nov 20 '18 at 22:01
add a comment |
You can use below one
select count(1) from table_name where status='NEW' group by Status_Date
or you can use this one to get count for all status in all dates
select count(1) as count,Status,left(Status_Date,10) as Status_Date from table_name
group by Status,left(Status_Date,10)
I failed to mention the dates are really a time line that only get updated when the order switches to a new status. So if the date is midnight on 9/20/2018, I only have 2 orders in SENT status as the first order, has moved onto FILL status
– mlwrig2
Nov 15 '18 at 18:44
@mlwrig2 that is an important requirement. Please edit that into your question so that it will be seen
– Hans Kesting
Nov 15 '18 at 18:55
Thanks, can't tell I'ma newbie can you :)
– mlwrig2
Nov 15 '18 at 19:09
add a comment |
You can use below one
select count(1) from table_name where status='NEW' group by Status_Date
or you can use this one to get count for all status in all dates
select count(1) as count,Status,left(Status_Date,10) as Status_Date from table_name
group by Status,left(Status_Date,10)
I failed to mention the dates are really a time line that only get updated when the order switches to a new status. So if the date is midnight on 9/20/2018, I only have 2 orders in SENT status as the first order, has moved onto FILL status
– mlwrig2
Nov 15 '18 at 18:44
@mlwrig2 that is an important requirement. Please edit that into your question so that it will be seen
– Hans Kesting
Nov 15 '18 at 18:55
Thanks, can't tell I'ma newbie can you :)
– mlwrig2
Nov 15 '18 at 19:09
add a comment |
You can use below one
select count(1) from table_name where status='NEW' group by Status_Date
or you can use this one to get count for all status in all dates
select count(1) as count,Status,left(Status_Date,10) as Status_Date from table_name
group by Status,left(Status_Date,10)
You can use below one
select count(1) from table_name where status='NEW' group by Status_Date
or you can use this one to get count for all status in all dates
select count(1) as count,Status,left(Status_Date,10) as Status_Date from table_name
group by Status,left(Status_Date,10)
answered Nov 15 '18 at 18:14
EslamspotEslamspot
91
91
I failed to mention the dates are really a time line that only get updated when the order switches to a new status. So if the date is midnight on 9/20/2018, I only have 2 orders in SENT status as the first order, has moved onto FILL status
– mlwrig2
Nov 15 '18 at 18:44
@mlwrig2 that is an important requirement. Please edit that into your question so that it will be seen
– Hans Kesting
Nov 15 '18 at 18:55
Thanks, can't tell I'ma newbie can you :)
– mlwrig2
Nov 15 '18 at 19:09
add a comment |
I failed to mention the dates are really a time line that only get updated when the order switches to a new status. So if the date is midnight on 9/20/2018, I only have 2 orders in SENT status as the first order, has moved onto FILL status
– mlwrig2
Nov 15 '18 at 18:44
@mlwrig2 that is an important requirement. Please edit that into your question so that it will be seen
– Hans Kesting
Nov 15 '18 at 18:55
Thanks, can't tell I'ma newbie can you :)
– mlwrig2
Nov 15 '18 at 19:09
I failed to mention the dates are really a time line that only get updated when the order switches to a new status. So if the date is midnight on 9/20/2018, I only have 2 orders in SENT status as the first order, has moved onto FILL status
– mlwrig2
Nov 15 '18 at 18:44
I failed to mention the dates are really a time line that only get updated when the order switches to a new status. So if the date is midnight on 9/20/2018, I only have 2 orders in SENT status as the first order, has moved onto FILL status
– mlwrig2
Nov 15 '18 at 18:44
@mlwrig2 that is an important requirement. Please edit that into your question so that it will be seen
– Hans Kesting
Nov 15 '18 at 18:55
@mlwrig2 that is an important requirement. Please edit that into your question so that it will be seen
– Hans Kesting
Nov 15 '18 at 18:55
Thanks, can't tell I'ma newbie can you :)
– mlwrig2
Nov 15 '18 at 19:09
Thanks, can't tell I'ma newbie can you :)
– mlwrig2
Nov 15 '18 at 19:09
add a comment |
If you want such a specific counter:
SELECT COUNT(*) AS counter FROM orders WHERE Status = 'SENT' and Status_Date <= '2012-09-16 23:59:59.000'
or
SELECT COUNT(*) AS counter FROM orders WHERE Status = 'SENT' and Status_Date <= Convert(datetime, '2012-09-16 23:59:59.000')
to convert the string literal to date.
add a comment |
If you want such a specific counter:
SELECT COUNT(*) AS counter FROM orders WHERE Status = 'SENT' and Status_Date <= '2012-09-16 23:59:59.000'
or
SELECT COUNT(*) AS counter FROM orders WHERE Status = 'SENT' and Status_Date <= Convert(datetime, '2012-09-16 23:59:59.000')
to convert the string literal to date.
add a comment |
If you want such a specific counter:
SELECT COUNT(*) AS counter FROM orders WHERE Status = 'SENT' and Status_Date <= '2012-09-16 23:59:59.000'
or
SELECT COUNT(*) AS counter FROM orders WHERE Status = 'SENT' and Status_Date <= Convert(datetime, '2012-09-16 23:59:59.000')
to convert the string literal to date.
If you want such a specific counter:
SELECT COUNT(*) AS counter FROM orders WHERE Status = 'SENT' and Status_Date <= '2012-09-16 23:59:59.000'
or
SELECT COUNT(*) AS counter FROM orders WHERE Status = 'SENT' and Status_Date <= Convert(datetime, '2012-09-16 23:59:59.000')
to convert the string literal to date.
answered Nov 15 '18 at 18:26
forpasforpas
18.3k3728
18.3k3728
add a comment |
add a comment |
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.
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%2fstackoverflow.com%2fquestions%2f53325480%2fcount-the-number-of-items-in-a-certain-status-for-a-given-date%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
what have you tried so far?
– Vamsi Prabhala
Nov 15 '18 at 18:12
Basically I somehow need the last status (top 1) for each order-id where the status date is < than a given date. Its selecting the top 1 for each order-id that is given me trouble
– mlwrig2
Nov 20 '18 at 21:54