如何从历史select天数table?

How to select number of days from history table?


已编辑:历史记录 table 可能包含对时间计算没有影响的行(例如评论或发送提醒...)

我有 2 个 table 结构如下

PS:为了简单起见,我避免了过多的细节(实际上,我有另一个 table 用于状态代码,另一个用于用户等)


门票Table

--------------------------------------------------
| ID  | Ticket_Details   | Issued_By | Status    |
--------------------------------------------------
| 001 | 'PC not working' | 'John'    | On Hold   |
| 002 | 'Printer broken' | 'Mike'    | Rejected  |
| 003 | 'Network down'   | 'Alex'    | Submitted |
| ..  | ...              | ..        | ...       |
--------------------------------------------------

历史Table

------------------------------------------------------------------------
| ID | Ticket_ID  | Ticket_Status | History_Details      | Insert_Date |
------------------------------------------------------------------------
| 01 | 001        | new           | submitted            | 23-Feb-2015 |
| 02 | 001        | submitted     | assigned to [Frank]  | 25-Feb-2015 |
| 03 | 001        | submitted     | commented 'needs ti.'| 25-Feb-2015 |
| 04 | 001        | assigned      | put on hold by[Frank]| 26-Feb-2015 |
| 05 | 001        | on hold       | reminder sent        | 01-Mar-2015 |
| 06 | 002        | new           | submitted            | 23-Feb-2015 |
| 07 | 002        | submitted     | rejected by [Sam]    | 24-Feb-2015 |
| 08 | 003        | new           | submitted            | 25-Feb-2015 |
------------------------------------------------------------------------

历史的每一行table包含以下信息:

1- id (auto inc) 主键

2- 工单 ID 的外键

3- 工单修改前的状态

4- 对工单执行的操作的备注

5- 动作的日期[和时间]

注意,有些行描述了状态的变化,而有些行则没有变化。仅发送评论或提醒,而工单保持其状态。

现在我想要实现的是 table,它显示工单的当前状态以及分配给该状态的天数。所以对于上面的示例数据,输出应该是:(考虑到今天的日期是 1-Mar-2015)


期望的输出

--------------------------------------------------
| ID  | Ticket_Details   | Status    | Since     |
--------------------------------------------------
| 001 | 'PC not working' | On Hold   | 3 days    |
| 002 | 'Printer broken' | Rejeced   | 5 days    |
| 003 | 'Network down'   | submitted | 4 days    |
| ..  | ...              | ..        |           |
--------------------------------------------------

基本上,我使用 history table 中 insert_date 中存储的信息来确定工单处于该状态的时间。

这是我尝试过的:

select 
   t.id,
   t.ticket_details,
   t.status,
   datediff(day, h.insert_date, getdate() ) "since"
from
         tickets t 
   left join history h on t.id = h.ticket_id
             and h.id = ( select max(id) from history h2 
                          where h2.ticket_id = h.ticket_id 
                                AND h2.ticket_status = t.status )

我告诉 SQL 在 history 中最后一次查找此票证 status..

但我得到了不准确的数据,因为一些 "since" 值出现了 nulls

我做错了什么?

也许试试

select 
   t.id,
   t.ticket_details,
   t.status,
   datediff(day, h.insert_date, getdate() ) "since"
from
         tickets t 
   left join (SELECT MAX(insert_date) as insert_date,ticket_id
               FROM history group by ticket_id) as h 
              on t.id=h.ticket_id

我觉得这个很好用 cross apply:

select t.*, datediff(day, minid, getdate()) as days_since
from tickets t outer apply
     (select min(insert_date) as minid
      from history h
      where h.ticket_id = t.ticket_id and h.ticket_status = t.status
     ) h;

注意:这是 return 工单处于当前状态的最早日期。这假设门票永远不会 return 到以前的状态,这与您问题中的信息一致。