Count rows after non zero

  • How to count rows after first non zero in a set? Example in the set data that have 2 column month and quantity, I want to start the count from the third row. The result should be 5. The query should ignore the top rows that has zero till a non zero number and count all rows after that even if there is zero.

      MonthsQuantity

      2014-04-010

      2014-05-010

      2014-06-015

      2014-07-010

      2014-08-014

      2014-09-0165

      2014-10-013

  • What is your expected output?

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • The output of the sample data set in question should be 5.

  • You can add a row number to each line with ROW_NUMBER(). Filter on non-zero quantities and get the minimum row number. Substract this from the maximum row number.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Koen Verbeeck (10/22/2014)


    You can add a row number to each line with ROW_NUMBER(). Filter on non-zero quantities and get the minimum row number. Substract this from the maximum row number.

    hi Thanks. I have tested it and your idea works, just that it would be "Subtract the filtered minimum row number - 1 from the maximum. This forum rocks!

  • saxsena_sanjay (10/22/2014)


    Koen Verbeeck (10/22/2014)


    You can add a row number to each line with ROW_NUMBER(). Filter on non-zero quantities and get the minimum row number. Substract this from the maximum row number.

    hi Thanks. I have tested it and your idea works, just that it would be "Subtract the filtered minimum row number - 1 from the maximum. This forum rocks!

    Yeah, I was not sure if you had to subtract 1 from the minimum row number or not and didn't have time to test it all out.

    Glad your problem is solved.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply