计算时间段内特定值的变化

时间:2014-07-12 10:16:03

标签: sql ms-access

MS Access查询问题。

在简化版中,我有一个包含(支持)ticketid列,状态列和时间戳列的表。时间戳列指示何时为故障单设置(新)状态。

我的最终目标是计算在特定时间范围内更改状态FROM状态18的故障单数量。这包括在时间范围内获得状态18并且再次“丢失”的票证,以及在时间帧开始时已经具有状态18的票证。

我的中间查询显然不正确,因为它返回太多记录。它不仅与下一个记录(时间)相比,而且与任何记录相比。

SELECT *
    FROM 
(SELECT tkhtimestamp, tkhticketid, tkhstatusid FROM tickethistory WHERE tkhstatusid NOT IN     (18) AND tkhtimestamp >= #3-jul-2014 9:00:00# AND tkhtimestamp <= #4-jul-2014 9:00:00#) AS new_status_records
    INNER JOIN 
(SELECT tkhtimestamp, tkhticketid, tkhstatusid FROM tickethistory WHERE  tkhstatusid  IN (18) AND tkhtimestamp <= #4-jul-2014 9:00:00#) AS old_status_records
    ON old_status_records.tkhticketid = new_status_records.tkhticketid AND old_status_records.tkhtimestamp < new_status_records.tkhtimestamp

有人可以就此提出建议,因为我似乎无法完成这项工作。使用max / min和group by的其他工作也失败了。

谢谢。

1 个答案:

答案 0 :(得分:0)

你有正确的想法使用MAX / MIN和GROUP BY。对于[tickethistory]中的测试数据

tkhtimestamp         tkhticketid  tkhstatusid
-------------------  -----------  -----------
2014-07-02 10:00:00            1           10
2014-07-02 12:00:00            2           18
2014-07-03 13:30:00            1           18
2014-07-03 13:35:00            1           99
2014-07-03 15:00:00            2           99

以下查询将提取每张故障单的每个新历史记录的当前和上一个时间戳:

SELECT 
    curr.tkhticketid, 
    curr.tkhtimestamp AS curr_timestamp, 
    MAX(prev.tkhtimestamp) AS prev_timestamp
FROM
    tickethistory curr
    INNER JOIN
    tickethistory prev
        ON curr.tkhticketid = prev.tkhticketid
            AND curr.tkhtimestamp > prev.tkhtimestamp
GROUP BY    
    curr.tkhticketid, 
    curr.tkhtimestamp

返回

tkhticketid  curr_timestamp       prev_timestamp     
-----------  -------------------  -------------------
          1  2014-07-03 13:30:00  2014-07-02 10:00:00
          1  2014-07-03 13:35:00  2014-07-03 13:30:00
          2  2014-07-03 15:00:00  2014-07-02 12:00:00

然后我们可以将其加入[tickethistory]表(两次)以检索当前和以前的状态值

SELECT
    tkh_curr.tkhticketid,
    tkh_curr.tkhtimestamp AS curr_timestamp,
    tkh_curr.tkhstatusid AS curr_status,
    tkh_prev.tkhtimestamp AS prev_timestamp,
    tkh_prev.tkhstatusid AS prev_status
FROM
    (
        (
            SELECT 
                curr.tkhticketid, 
                curr.tkhtimestamp AS curr_timestamp, 
                MAX(prev.tkhtimestamp) AS prev_timestamp
            FROM
                tickethistory curr
                INNER JOIN
                tickethistory prev
                    ON curr.tkhticketid = prev.tkhticketid
                        AND curr.tkhtimestamp > prev.tkhtimestamp
            GROUP BY    
                curr.tkhticketid, 
                curr.tkhtimestamp
        ) curr_prev
        INNER JOIN
        tickethistory tkh_curr
            ON tkh_curr.tkhticketid = curr_prev.tkhticketid
                AND tkh_curr.tkhtimestamp = curr_prev.curr_timestamp
    )
    INNER JOIN
    tickethistory tkh_prev
        ON tkh_prev.tkhticketid = curr_prev.tkhticketid
            AND tkh_prev.tkhtimestamp = curr_prev.prev_timestamp

返回

tkhticketid  curr_timestamp       curr_status  prev_timestamp       prev_status
-----------  -------------------  -----------  -------------------  -----------
          1  2014-07-03 13:30:00           18  2014-07-02 10:00:00           10
          1  2014-07-03 13:35:00           99  2014-07-03 13:30:00           18
          2  2014-07-03 15:00:00           99  2014-07-02 12:00:00           18

如果您使用[qryTicketHistoryChanges]之类的名称在Access中保存该查询,那么您应该能够针对它运行其他查询以提取您需要的特定信息。