如何找到同一列中两个日期之间的差异?

时间:2013-04-03 06:44:21

标签: sql sql-server sql-server-2008 datediff sliding-window

我有一个表SO_STATUS,它为服务订单的每个状态更改写入一条记录(我们将调用Service_Order_ID“Job_ID”)。 Job_ID引用SERVICE_ORDER表。初始化服务订单时,将为该状态类型“打开”(状态类型2)写入记录,该记录显示日期时间。然后在状态表中写入另一条记录,用于何时“正在进行”(StatusType 1)。而且当服务订单“关闭”时,另一条记录写在状态表(StatusType 3)中。还有其他可能发生的状态类型,但这些是最常见的。 SO_STATUS表中的数据如下所示:

id            Date                Job_ID  StatusTypeID  EmployeeID
1     2012-01-01 09:05:00.000       51        2             5
2     2012-01-01 10:00:00.000       52        2            12
3     2012-01-01 10:01:00.000       51        1             5
4     2012-01-01 12:15:00.000       53        2             8
5     2012-01-01 12:16:00.000       51        3             5
6     2012-01-01 13:00:00.000       52        1            12
7     2012-01-01 14:00:00.000       52        3            12
8     2012-01-01 14:15:00.000       53        1             8
9     2012-01-01 15:00:00.000       54        2            11
10    2012-01-01 16:30:00.000       53        3             8
11    2012-01-01 15:00:00.000       54        1            11
12    2012-01-01 16:30:00.000       54        3            11

我需要能够找到每个Job_ID的每次状态更改之间经过的时间。基本上,从工作的开放到结束所花费的时间。

输出看起来像(从EMPLOYEE表中引用EmployeeName):

Job_ID       Duration    EmployeeName
  51         03:11:00        Kyle
  52         04:00:00        Chris
  53         04:15:00        Fred
  54         01:30:00        John

我将如何获得此类输出?谢谢。

7 个答案:

答案 0 :(得分:2)

为什么不使用:

SELECT DATEDIFF (anyparticularunit, ' 2012-01-01 09:05:00.000', ' 2012-01-01 15:00:00.000')

浏览dateiff的以下链接:

http://msdn.microsoft.com/en-us/library/ms189794.aspx

请点击此链接获取不同的例子:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=56126

希望你能在条件更进一步的地方。

答案 1 :(得分:1)

这一个 -

SET NOCOUNT ON;

DECLARE @duration TABLE 
(
      id BIGINT IDENTITY
    , [date] DATETIME
    , job_id INT
    , [status] VARCHAR(10)
    , employee_id INT
)

INSERT INTO @duration ([date], job_id, [status], employee_id)
VALUES
    ('2012-01-01 09:05:00.000', 51, 'open', 5),
    ('2012-01-01 10:00:00.000', 52, 'open', 12),
    ('2012-01-01 10:01:00.000', 51, 'inprogress', 5),
    ('2012-01-01 12:15:00.000', 53, 'open', 8),
    ('2012-01-01 12:16:00.000', 51, 'closed', 5),
    ('2012-01-01 13:00:00.000', 52, 'inprogress', 12),
    ('2012-01-01 14:00:00.000', 52, 'closed', 12),
    ('2012-01-01 14:15:00.000', 53, 'inprogress', 8),
    ('2012-01-01 15:00:00.000', 54, 'open', 11),
    ('2012-01-01 16:30:00.000', 53, 'closed', 8),
    ('2012-01-01 15:00:00.000', 54, 'inprogress', 11),
    ('2012-01-01 16:30:00.000', 54, 'closed', 11)

SELECT 
      job_id
    , employee_id
    , work_time = CONVERT(VARCHAR(12), MAX([date]) - MIN([date]), 114) 
FROM @duration
GROUP BY job_id, employee_id

答案 2 :(得分:0)

您可以使用DATEDIFF返回指定的startdate和enddate之间交叉的指定日期部分边界的计数(有符号整数)(请参阅http://msdn.microsoft.com/en-us/library/ms189794.aspx

 SELECT Job_ID,
 DATEDIFF(day, (SELECT MIN(Date) FROM YOUTABLE WHERE Job_ID=k.Job_ID),(SELECT MAX(Date) FROM YOUTABLE WHERE Job_ID=k.Job_ID)),
 (SELECT EmployeeName FROM EmployeeTABLE WHERE EmployeeID=k.EmployeeID)) FROM YOUTABLE k

答案 3 :(得分:0)

如果您的数据库是Oracle,您可以这样做

SELECT DISTINCT JOB_ID, MAX(DATE) OVER(PARTITION BY JOB_ID)-MIN(DATE) OVER(PARTITION BY JOB_ID) AS Duration FROM TA JOIN TB .....

答案 4 :(得分:0)

我创建了一些自定义代码来创建数据和时间差,使用datediff函数并用特定数字除以生成小时,分钟和秒:

SELECT 
    Job_ID,
    CAST(DATEDIFF(second, MIN(Date), MAX(Date)) / 3600 AS VARCHAR)
    + ':' + CAST((DATEDIFF(second, MIN(Date), MAX(Date)) % 3600) / 60 AS VARCHAR)
    + ':' + CAST(((DATEDIFF(second, MIN(Date), MAX(Date)) % 3600) % 60)  AS VARCHAR)
FROM YOUTABLE 
GROUP BY Job_ID

答案 5 :(得分:0)

尝试下面的查询:

Select t1.Job_ID,
Convert(varchar(5),DateDiff(HH,Min(t1.JobDate),tbl.MaxDate))+' : '+convert(varchar(5),DateDiff(s,Min(t1.JobDate),tbl.MaxDate) % 3600/60)+' : '+Convert(varchar(5),DateDiff(s,Min(t1.JobDate),
tbl.MaxDate) % 60) MinDate,t1.EmployeeName From SO_STATUS t1
Inner join (Select Max(JobDate) MaxDate, job_id From SO_STATUS  Group By Job_Id)tbl on t1.Job_ID=tbl.Job_ID
Inner Join EMPLOYEE e On e.EmployeeID=t1.EmployeeID
Group By t1.EmployeeName,tbl.MaxDate,t1.Job_ID
Order By t1.Job_ID

答案 6 :(得分:0)

具有日期时间格式的不同表的两个日期之间的差异。

SELECT t1.Column_Names,

CONVERT(VARCHAR(10),t1.CreatedOn,103)

AS CreatedOn FROM table1 t1 INNER JOIN table2 t2

ON t1.id = t2.id 在哪里CAST(t1.CreatedOn as Date)

BETWEEN @fromdate和@todate。

  

我把t1.CreatedOn作为我的table属性保存日期。

     

@fromdate和@todate传递日期。