Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 在sql server 2008中计算票证花费的总时间_Sql Server 2008 - Fatal编程技术网

Sql server 2008 在sql server 2008中计算票证花费的总时间

Sql server 2008 在sql server 2008中计算票证花费的总时间,sql-server-2008,Sql Server 2008,我有一个SQL Server 2008中票证历史记录的日志表。这里我只有datetime和Status文件。我需要计算状态之间的总时间 示例: 我在2015-06-19 15:50:44.000打开了一张票,之后我在2015-06-22 11:15:15.000将其更改为“正在工作”。两个时间戳值将位于同一DateTime字段下。我想计算状态更改之间的时间差。状态将随机更改 ticket_number Date & Time Problem Status 1667

我有一个SQL Server 2008中票证历史记录的日志表。这里我只有datetime和Status文件。我需要计算状态之间的总时间

示例:
我在
2015-06-19 15:50:44.000打开了一张票,之后我在
2015-06-22 11:15:15.000将其更改为“正在工作”。两个时间戳值将位于同一DateTime字段下。我想计算状态更改之间的时间差。状态将随机更改

ticket_number   Date & Time           Problem Status
16676       2015-06-19 15:50:14.000    Open
16676       2015-06-19 15:50:14.000    Accepted
16676       2015-06-19 15:50:44.000    Work in progress
16676       2015-06-19 16:03:13.000    Pending Vendor
16676       2015-06-22 06:32:31.000    Work in progress
16676       2015-06-22 11:15:15.000    Pending Vendor
16676       2015-06-23 10:15:15.000    Work in progress
16676       2015-06-23 10:15:15.000    Closed
在这里,我必须计算从接受状态到关闭状态的总时间,我们必须排除待定的供应商状态时间


提前感谢

如果每张票只有两个条目,那么这就相当简单了。由于您没有发布任何样本数据或模式信息,因此我们必须在此处做出一些假设,例如:

Select   op.ticket_number
       , datediff(minute, wip.timestamp, op.timestamp) as diff_minutes
from   yourTable op
       inner join yourTable wip
           on op.ticket_number = wip.ticket_number
where  wip.status = 'Work_in_progress'
and    op.status = 'Opened'
order by op.ticket_number
这将把表中打开状态为“op”的记录子集和另一个状态为“正在工作”的数据子集视为“wip”。如果在票号上将这些时间加在一起,则可以计算两个相应时间戳之间的分钟(或小时、天、秒等)差。
如果您有两个以上的状态需要处理,那么这会变得更复杂,其他方法可能更合适。在这种情况下,请发布一些示例数据和相关表的架构。

如果每个票证只有2个条目,那么这是相当简单的。由于您没有发布任何样本数据或模式信息,因此我们必须在此处做出一些假设,例如:

Select   op.ticket_number
       , datediff(minute, wip.timestamp, op.timestamp) as diff_minutes
from   yourTable op
       inner join yourTable wip
           on op.ticket_number = wip.ticket_number
where  wip.status = 'Work_in_progress'
and    op.status = 'Opened'
order by op.ticket_number
这将把表中打开状态为“op”的记录子集和另一个状态为“正在工作”的数据子集视为“wip”。如果在票号上将这些时间加在一起,则可以计算两个相应时间戳之间的分钟(或小时、天、秒等)差。
如果您有两个以上的状态需要处理,那么这会变得更复杂,其他方法可能更合适。在这种情况下,请发布一些示例数据和相关表格的模式。

将此作为单独的答案添加,因为方法完全不同。很确定这就是你想要的:

-- Create table for sample data
Create table #tickets
(   ticket_number   int
,   dateTime_status datetime
,   ProblemStatus   nvarchar(50)
)

-- Insert sample data into temp table
Insert into #tickets
Values  (16676, '2015-06-19 15:50:14.000', 'Open')
    ,   (16676, '2015-06-19 15:50:14.000', 'Accepted')
    ,   (16676, '2015-06-19 15:50:44.000', 'Work in progress')
    ,   (16676, '2015-06-19 16:03:13.000', 'Pending Vendor')
    ,   (16676, '2015-06-22 06:32:31.000', 'Work in progress') 
    ,   (16676, '2015-06-22 11:15:15.000', 'Pending Vendor')
    ,   (16676, '2015-06-23 10:15:15.000', 'Work in progress') 
    ,   (16676, '2015-06-23 10:15:15.000', 'Closed')
    ,   (16677, '2015-06-19 15:50:14.000', 'Open')
    ,   (16677, '2015-06-20 15:50:14.000', 'Accepted')
    ,   (16677, '2015-06-20 15:50:44.000', 'Work in progress')
    ,   (16677, '2015-06-20 16:03:13.000', 'Pending Vendor')
    ,   (16677, '2015-06-23 06:32:31.000', 'Work in progress') 
    ,   (16677, '2015-06-23 11:15:15.000', 'Pending Vendor')
    ,   (16677, '2015-06-24 10:15:15.000', 'Work in progress') 
    ,   (16677, '2015-06-25 10:15:15.000', 'Closed')

-- Select time difference for status from and to per ticket
-- Time difference is measured in minutes. Change the datepare to desired unit if required.
Select      ROW_NUMBER() over(partition by a.ticket_number order by a.RowNum)
        ,   a.ticket_number
        ,   a.ProblemStatus as 'Status_from'
        ,   b.ProblemStatus  as 'Status_to'
        ,   DATEDIFF(MINUTE,a.dateTime_status,b.dateTime_status) as 'Time_in_minutes'
from    
        -- Nested select to set subsequent events side by side. Add row numbers.
        (Select     ROW_NUMBER() over(partition by ticket_number order by dateTime_status) as 'RowNum'
                ,   ticket_number
                ,   dateTime_status
                ,   ProblemStatus
        from    #tickets) as A

        -- Join first nested query on second one with same data, but join on b.rownum = a.rownum + 1
        -- This puts the event in B next to the previous event in A
        inner join (Select  ROW_NUMBER() over(partition by ticket_number order by dateTime_status) as 'RowNum'
                        ,   ticket_number
                        ,   dateTime_status
                        ,   ProblemStatus
                    from    #tickets) as B
            on a.ticket_number = b.ticket_number
            and b.RowNum = a.RowNum + 1
-- Exclude records where the beginning status is "Pending Vendor"
where   a.ProblemStatus <> 'Pending Vendor'
order by    a.ticket_number
        ,   a.RowNum

-- Drop the temp table
drop table #tickets
——为样本数据创建表格
创建表#票证
(车票号码)
,dateTime\u状态dateTime
,问题状态nvarchar(50)
)
--将样本数据插入临时表
插入#票据
数值(16676,'2015-06-1915:50:14.000,'Open')
,(16676,'2015-06-1915:50:14.000,'
,(16676,'2015-06-19 15:50:44.000,'Work in progress')
,(16676,'2015-06-19 16:03:13.000,'待定供应商')
,(16676,'2015-06-22 06:32:31.000,'Work in progress')
,(16676,'2015-06-22 11:15:15.000,'待定供应商')
,(16676,'2015-06-23 10:15:15.000,'Work in progress')
,(16676,'2015-06-23 10:15:15.000,'Closed')
,(16677,'2015-06-1915:50:14.000,'Open')
,(16677,“2015-06-20 15:50:14.000”,“接受”)
,(16677,'2015-06-20 15:50:44.000,'Work in progress')
,(16677,“2015-06-20 16:03:13.000”,“待定供应商”)
,(16677,'2015-06-23 06:32:31.000,'Work in progress')
,(16677,“2015-06-23 11:15:15.000”,“待定供应商”)
,(16677,'2015-06-2410:15:15.000,'Work in progress')
,(16677,'2015-06-25 10:15:15.000,'Closed')
--为每张票证的状态从和到选择时差
--时差以分钟为单位。如果需要,将日期更改为所需单位。
选择上面的行数()(按a.ticket分区,按a.RowNum排序)
,a.车票号码
,a.问题状态为“状态\来源”
,b.问题状态为“状态到”
,DATEDIFF(分钟,a.dateTime\u状态,b.dateTime\u状态)为“时间(单位:分钟)”
从…起
--嵌套选择以并排设置后续事件。添加行号。
(选择(按票证编号顺序按日期时间状态划分)上方的行编号()作为“RowNum”
,车票号码
,dateTime_状态
,问题状态
从#票)作为
--在第二个嵌套查询上使用相同的数据联接第一个查询,但在b.rownum=a.rownum+1上联接
--这会将B中的事件置于A中上一个事件的旁边
内部联接(选择ROW_NUMBER()作为“RowNum”
,车票号码
,dateTime_状态
,问题状态
从#票)到B
a.票号=b.票号
和b.RowNum=a.RowNum+1
--排除开始状态为“待定供应商”的记录
其中a.ProblemStatus“待定供应商”
按票号订购
,a.RowNum
--放下临时表
投递台#票

基本上,仍然可以将相同的数据作为两个独立的数据集相互连接,但通过向数据中添加行号,可以将一个表连接到第二个表中的下一行。这允许您并排设置顺序事件,并计算它们之间的时间差。排除第一个表中ProblemStatus为“待定供应商”且您有解决问题的实际时间的任何记录。分钟的总和是每张票的总时间。如果需要更精确的时间测量,请将datepart更改为秒。然后很容易将其转换为秒、分钟、小时格式。

将其作为单独的答案添加,因为方法完全不同。很确定这就是你想要的:

-- Create table for sample data
Create table #tickets
(   ticket_number   int
,   dateTime_status datetime
,   ProblemStatus   nvarchar(50)
)

-- Insert sample data into temp table
Insert into #tickets
Values  (16676, '2015-06-19 15:50:14.000', 'Open')
    ,   (16676, '2015-06-19 15:50:14.000', 'Accepted')
    ,   (16676, '2015-06-19 15:50:44.000', 'Work in progress')
    ,   (16676, '2015-06-19 16:03:13.000', 'Pending Vendor')
    ,   (16676, '2015-06-22 06:32:31.000', 'Work in progress') 
    ,   (16676, '2015-06-22 11:15:15.000', 'Pending Vendor')
    ,   (16676, '2015-06-23 10:15:15.000', 'Work in progress') 
    ,   (16676, '2015-06-23 10:15:15.000', 'Closed')
    ,   (16677, '2015-06-19 15:50:14.000', 'Open')
    ,   (16677, '2015-06-20 15:50:14.000', 'Accepted')
    ,   (16677, '2015-06-20 15:50:44.000', 'Work in progress')
    ,   (16677, '2015-06-20 16:03:13.000', 'Pending Vendor')
    ,   (16677, '2015-06-23 06:32:31.000', 'Work in progress') 
    ,   (16677, '2015-06-23 11:15:15.000', 'Pending Vendor')
    ,   (16677, '2015-06-24 10:15:15.000', 'Work in progress') 
    ,   (16677, '2015-06-25 10:15:15.000', 'Closed')

-- Select time difference for status from and to per ticket
-- Time difference is measured in minutes. Change the datepare to desired unit if required.
Select      ROW_NUMBER() over(partition by a.ticket_number order by a.RowNum)
        ,   a.ticket_number
        ,   a.ProblemStatus as 'Status_from'
        ,   b.ProblemStatus  as 'Status_to'
        ,   DATEDIFF(MINUTE,a.dateTime_status,b.dateTime_status) as 'Time_in_minutes'
from    
        -- Nested select to set subsequent events side by side. Add row numbers.
        (Select     ROW_NUMBER() over(partition by ticket_number order by dateTime_status) as 'RowNum'
                ,   ticket_number
                ,   dateTime_status
                ,   ProblemStatus
        from    #tickets) as A

        -- Join first nested query on second one with same data, but join on b.rownum = a.rownum + 1
        -- This puts the event in B next to the previous event in A
        inner join (Select  ROW_NUMBER() over(partition by ticket_number order by dateTime_status) as 'RowNum'
                        ,   ticket_number
                        ,   dateTime_status
                        ,   ProblemStatus
                    from    #tickets) as B
            on a.ticket_number = b.ticket_number
            and b.RowNum = a.RowNum + 1
-- Exclude records where the beginning status is "Pending Vendor"
where   a.ProblemStatus <> 'Pending Vendor'
order by    a.ticket_number
        ,   a.RowNum

-- Drop the temp table
drop table #tickets
——为样本数据创建表格
创建表#票证
(车票号码)
,dateTime\u状态dateTime
,问题状态nvarchar(50)
)
--将样本数据插入临时表
插入#票据
数值(16676,'2015-06-1915:50:14.000,'Open')
,