Sub query to find sum of work order cancels

时间:2015-12-10 01:21:03

标签: sql teradata

I've been learning SQL for a little bit now with work and I am having a lot of trouble getting this query together. Here is the question I am trying to answer:

Out of the customers who cancel a work order (between 08/16/2015 and 12/07/2015), how many setup a work order within a week?

Here is my query right now (it isn't finished):

SELECT a.create_date as "date"
    ,   COUNT(*) as "total_work_orders"
    ,   SUM (
            CASE WHEN a.status = 'X' THEN 1 ELSE 0 END
        ) as "total_canceled"
FROM ALL_WORK_ORDERS a
WHERE ("date" >= CAST('08-16-2015' AS DATE FORMAT 'MM-DD-YYYY'))
AND ("date" <= CAST('12-07-2015' AS DATE FORMAT 'MM-DD-YYYY'))
GROUP BY "date"
ORDER BY "date"

This pulls the total amount of work orders as well as the total that have been canceled. I am thinking that I need to perform a sub query that will run when a work order has been found to be 'X' (canceled), and then count how many work orders (if any), have been 'O' (Opened), within a week of the cancel date. I think I should also use a.completion_date to grab the date the initial work order was canceled.

I just can't figure out how to put it all together. I would greatly appreciate any and all help.

Thank you.

EDIT: After further insight, here are some clarifying details. The query will provide a count of the total for each day. An example is if a work order is created on 10/08 and then canceled on 10/09. But they create a new work order on 10/12. Then there would be a +1 for 10/08 in the output.

I made a query earlier that pulls canceled work orders over a week and I figured I could pull that apart to work with this.

SELECT a.create_date as "date",
   COUNT(*) AS "total_work_orders",
   SUM(CASE WHEN  a.create_date = a.completion_date AND a.status = 'X'
         THEN 1 ELSE 0  END) AS "cancelled same day",
   SUM(CASE WHEN  a.create_date + INTERVAL '1' DAY = a.completion_date AND a.status = 'X'
        THEN 1 ELSE 0  END) AS "cancelled next day",
   SUM(CASE WHEN a.create_date + INTERVAL '2' DAY =  a.completion_date AND a.status = 'X'
        THEN 1 ELSE 0  END) AS "cancelled +2 Days",
   SUM(CASE WHEN  a.create_date + INTERVAL '3' DAY = a.completion_date  AND a.status = 'X'
        THEN 1 ELSE 0  END) AS "cancelled +3 Days",
   SUM(CASE WHEN  a.create_date + INTERVAL '4' DAY = a.completion_date  AND a.status = 'X'
        THEN 1 ELSE 0  END) AS "cancelled +4 Days",
   SUM(CASE WHEN a.create_date + INTERVAL '5' DAY = a.completion_date  AND a.status = 'X'
        THEN 1 ELSE 0  END) AS "cancelled +5 Days",
   SUM(CASE WHEN  a.create_date + INTERVAL '6' DAY = a.completion_date  AND a.status = 'X'
        THEN 1 ELSE 0  END) AS "cancelled +6 Days",
   SUM(CASE WHEN  a.create_date + INTERVAL '7' DAY = a.completion_date  AND a.status = 'X'
        THEN 1 ELSE 0  END) AS "cancelled +7 Days"
FROM    ALL_WORK_ORDERS a 
WHERE ( a.create_date >= CAST('08-16-2015' AS DATE FORMAT 'MM-DD-YYYY'))
AND ( a.create_date <=  CAST('12-07-2015' AS DATE FORMAT 'MM-DD-YYYY'))
GROUP BY "Date"
ORDER BY "Date"

I am fairly certain that I am on the right track. But I still consider myself an early beginner at SQL.

EDIT 2: I can't seem to describe this right. That is my bad. Here is a table of desired output.

desired output

1 个答案:

答案 0 :(得分:1)

如果性能不是问题,这可能会对您有所帮助:

(也许你需要解决这个问题,因为我并不习惯MS SQL)

SELECT 
    a.create_date AS 'date',
    COUNT(*) AS 'total_work_orders',
    b.counter AS '2nd wo within 7days',
    c.counter AS '2nd wo 8-14 days'
FROM
    ALL_WORK_ORDERS a
        INNER JOIN
    (SELECT 
        a.create_date AS 'date', COUNT(*) AS counter
    FROM
        ALL_WORK_ORDERS t1
    WHERE
        t1.completion_date BETWEEN t1.create_date + INTERVAL 1 DAY AND t1.create_date + INTERVAL 7 DAY
            AND t1.status = 'X'
    GROUP BY a.date) b ON a.date = b.date
        INNER JOIN
    (SELECT 
        a.create_date AS 'date', COUNT(*) AS counter
    FROM
        ALL_WORK_ORDERS t2
    WHERE
        t1.completion_date BETWEEN t2.create_date + INTERVAL 8 DAY AND t2.create_date + INTERVAL 14 DAY
            AND t2.status = 'X'
    GROUP BY a.date) c ON a.date = c.date
WHERE (a.date >= CAST('08-16-2015' AS DATE FORMAT 'MM-DD-YYYY'))
AND (a.date <= CAST('12-07-2015' AS DATE FORMAT 'MM-DD-YYYY'))
GROUP BY a.date
ORDER BY a.date;