SQL Count Of Open Orders Each Day Between Two Dates

后端 未结 4 1152
感情败类
感情败类 2020-12-21 04:38

I\'ve tried searching but it\'s likely I\'m using the wrong keywords as I can\'t find an answer.

I\'m trying to find the number of orders that are open between two d

4条回答
  •  执笔经年
    2020-12-21 05:03

    Calling the result column count was a bit odd because it seems to be in fact a row number. You can do that by using ROW_NUMBER.

    The other interesting part is that you also want open date and close date as separate rows. Using a simple UNION will solve that.

    WITH cte 
         AS (SELECT Row_number() OVER ( PARTITION BY employee  
                                        ORDER BY order_ref) count, 
                    employee, 
                    opened, 
                    closed 
             FROM   orders) 
    SELECT employee,  opened date,  count 
    FROM   cte 
    UNION ALL 
    SELECT employee,  closed date,  count 
    FROM   cte 
    ORDER  BY Date, 
              employee 
    

    DEMO

提交回复
热议问题