Count the number of items in a certain status for a given date










0















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'.










share|improve this question
























  • 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
















0















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'.










share|improve this question
























  • 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














0












0








0








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'.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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













3 Answers
3






active

oldest

votes


















0














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.






share|improve this answer

























  • 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


















-1














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)





share|improve this answer























  • 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


















-1














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.






share|improve this answer






















    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%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









    0














    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.






    share|improve this answer

























    • 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















    0














    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.






    share|improve this answer

























    • 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













    0












    0








    0







    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.






    share|improve this answer















    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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

















    • 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













    -1














    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)





    share|improve this answer























    • 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















    -1














    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)





    share|improve this answer























    • 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













    -1












    -1








    -1







    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)





    share|improve this answer













    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)






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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

















    • 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











    -1














    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.






    share|improve this answer



























      -1














      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.






      share|improve this answer

























        -1












        -1








        -1







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 18:26









        forpasforpas

        18.3k3728




        18.3k3728



























            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.




            draft saved


            draft discarded














            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





















































            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