如何获取日期范围的Open workorders数量

时间:2016-08-02 06:01:25

标签: sql sql-server count

工作订单表仅显示特定工单的当前状态,每当发生更改时,wostatus表都会显示工作订单的历史记录。任何不在"关闭"或者"完成"状态被视为"打开"。

所以,我试图找出在特定日期有多少工作人员处于开放状态,例如2016年7月7日,同样地,每个优先级在给定范围内的开放工作人员的数量。请检查样品输出。谢谢你,非常感谢任何帮助...

表:WorKOrder

 WoNum,     Priority,   UserID,         Status,             OpenDate,   Description,                            StatusDate, Location
 A17047,    12,         AD201685359,    WaitingMaterials,   30-Jun-16,  laptop fan not working,                 2-Jul-15,   HR
 A55198,    12,         AD201658376,    WaitingApproval,    01-Jul-16,  Need help with connecting two monitors, 1-Jul-16,   Finance
 A35508,    4,          AD201621226,    Close,              02-Jul-16,  Upgrade memory to 8GB,                  8-Jul-16,   Media

表:wostatus

Wonum,  Status,             changedate, changedby
A17047, InProgress,         30-Jun-16,  AD201659555
A17047, Approved,           01-Jul-16,  AD201659555
A17047, WaitingMaterials,   02-Jul-15,  AD201659555
A55198, InProgress,         01-Jul-16,  AD201659555
A55198, WaitingApproval,    01-Jul-16,  AD201659555
A35508, InProgress,         02-Jul-16,  AD201659555
A35508, WaitingApproval,    02-Jul-16,  AD201659555
A35508, Approved,           03-Jul-16,  AD201659555
A35508, Complete,           08-Jul-16,  AD201659555
A35508, Close,              08-Jul-16,  AD201659555 

查询输出/希望输出看起来像这样

Date, Total P12 Open WorkOrders,Total P4 Open WorkOrders
 30-Jun-16    1    0
 01-Jul-16    2    0
 02-Jul-16    2    1
 03-Jul-16    2    1
 04-Jul-16    2    1
 05-Jul-16    2    1
 06-Jul-16    2    1
 07-Jul-16    2    1
 08-Jul-16    2    0

1 个答案:

答案 0 :(得分:0)

我们必须分成几部分,我不能建议一个完整的答案,直到我知道某些部分有效。工作单表上没有关闭日期,这是一种耻辱(而且是意料之外的)。

首先要获得工作订单清单及其开启和关闭日期。试试这个查询,看看它是否有效

-- Query 1: Get a list of work order with open and close dates
-- excluding WO's that were already closed before 1 Jan 2016

SELECT 
WO.WONum, 
WO.OpenDate, 
ISNULL(
    WC.CloseDate,
    CONVERT(DATE,'20990101',112)
) as CloseDate
FROM WorKOrder WO
LEFT OUTER JOIN 
(
SELECT WONum, MAX(ChangeDate) as CloseDate
FROM WorKOrder
WHERE Status IN ('Close','Complete')
GROUP BY WONum
) WC
ON WO.WONum = WC.WONum
-- Exclude altogether any work orders that were closed prior to 1 Jan 2016
-- As these should not appear in the report
WHERE NOT EXISTS (
    SELECT *
    FROM WorKOrder WAC
    WHERE Status IN ('Close','Complete')
    AND ChangeDate <= CONVERT(DATE,'20160101',112)
    AND WAC.WONum = WO.WONum
)

我认为WONum在表WorkOrder上是唯一的?此查询应列出所有工作订单及其打开和关闭日期。如果工单仍处于打开状态,我们将使用截止日期为2099年1月1日的

第二个查询为我们提供了我们需要报告的所有日期。再次运行并确保其有效。还请说明我们在这里报道的天数?一年? 10?

改编自http://blog.waynesheffield.com/wayne/code-library/virtual-tally-numbers-table/

-- Query 2: Tally/calendar table
;WITH
TENS (N)      AS (SELECT 0 UNION ALL SELECT 0 UNION ALL
                  SELECT 0 UNION ALL SELECT 0 UNION ALL
                  SELECT 0 UNION ALL SELECT 0 UNION ALL
                  SELECT 0 UNION ALL SELECT 0 UNION ALL
                  SELECT 0 UNION ALL SELECT 0),
THOUSANDS (N) AS (SELECT 1
                    FROM TENS t1
                         CROSS JOIN TENS t2
                         CROSS JOIN TENS t3),
TALLY (N)     AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0))
                    FROM THOUSANDS)
SELECT TOP 1000 DATEADD(d,N,CONVERT(DATE,'20151231',112)) ADate
FROM TALLY;

让这两个查询先行,我们将从那里开始。

最后的查询将这两者结合在一起。这仍然没有完成,但会为您提供日期列表,以及在该日期仍有多少工作单仍未打开。请试试这个。如果它不起作用,我们将调试它。如果它确实有效,您必须测试您获得的数字。然后我们将添加P分解

-- Query 3: stick it all together
;WITH
TENS (N)      AS (SELECT 0 UNION ALL SELECT 0 UNION ALL
                  SELECT 0 UNION ALL SELECT 0 UNION ALL
                  SELECT 0 UNION ALL SELECT 0 UNION ALL
                  SELECT 0 UNION ALL SELECT 0 UNION ALL
                  SELECT 0 UNION ALL SELECT 0),
THOUSANDS (N) AS (SELECT 1
                    FROM TENS t1
                         CROSS JOIN TENS t2
                         CROSS JOIN TENS t3),
TALLY (N)     AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0))
                    FROM THOUSANDS)

SELECT 
WO.Priority,
CAL.D As IsOpenDate,
COUNT(*)
FROM WorKOrder WO
LEFT OUTER JOIN 
(
SELECT WONum, MAX(ChangeDate) as CloseDate
FROM WorKOrder
WHERE Status IN ('Close','Complete')
GROUP BY WONum
) WC
ON WO.WONum = WC.WONum
INNER JOIN 
(SELECT DATEADD(d,N,CONVERT(DATE,'20160101',112)) D FROM TALLY) CAL
ON CAL.D BETWEEN WO.OpenDate 
   AND   ISNULL(
            WC.CloseDate,
            CONVERT(DATE,'20990101',112)
         )
WHERE NOT EXISTS (
    SELECT *
    FROM WorKOrder WAC
    WHERE Status IN ('Close','Complete')
    AND ChangeDate <= CONVERT(DATE,'20160101',112)
    AND WAC.WONum = WO.WONum
)
GROUP BY CAL.D, WO.Priority

只是确认这是做什么的:对P4的数字为1,000,日期为27-08-2016意味着在此日期,在之前打开了P4的1,000个工单 on 27-08-2016,尚未关闭。因此,这是当天开放的工单的数量。这不是当天开放 ed 的数量。您不应该添加此数字,您应该在非加性趋势图中使用它

如果您想查看构成此编号的1,000个单独的工单,请使用查询1并将其添加到结尾:

AND CONVERT(DATE,'20160827',112) BETWEEN WO.OpenDate 
       AND   ISNULL(
                WC.CloseDate,
                CONVERT(DATE,'20990101',112)
             )