SQL时间戳减法获取天数

时间:2013-06-13 17:13:15

标签: sql sql-server sql-server-2008

在下面的代码而不是活跃的年龄我需要使用Open_time和当前时间进行计算。

换句话说,如果当前时间减去open_time在0到30之间,或者31到60之间,或者超过60之后,我需要这样做。

SELECT COUNT(CASE WHEN Assignment = 'Crosby' AND Severity = 4 AND CloseTime-OpenTime = 0-30 THEN P_NUMBER END) as crosby_sev4_030,
           COUNT(CASE WHEN Assignment = 'Crosby' AND Severity = 5 AND Active_Age='0-30' THEN P_NUMBER END) as crosby_sev5_030,
           COUNT(CASE WHEN Assignment = 'Crosby' AND Severity = 4 AND Active_Age='31-60' THEN P_NUMBER END) as crosby_sev4_3160,
           COUNT(CASE WHEN Assignment = 'Crosby' AND Severity = 5 AND Active_Age='31-60' THEN P_NUMBER END) as crosby_sev5_3160,
           COUNT(CASE WHEN Assignment = 'Crosby' AND Severity = 4 AND Active_Age='60+' THEN P_NUMBER END) as crosby_sev4_60,
           COUNT(CASE WHEN Assignment = 'Crosby' AND Severity = 5 AND Active_Age='60+' THEN P_NUMBER END) as crosby_sev5_60
    FROM dashboard.dbo.SmThings
    WHERE Assignment IN('Crosby')
    AND Severity IN(4,5)

3 个答案:

答案 0 :(得分:0)

如果您使用的是MSSQL - 请使用DATEDIFF

SELECT DATEDIFF(day,'2008-06-05','2008-08-05') AS DiffDate

输出将是

  

DiffDate
  61

答案 1 :(得分:0)

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

SELECT 
COUNT(CASE WHEN Assignment = 'Crosby' AND Severity = 4 AND datediff('day',getdate(), OpenTime) between 0 and 30 THEN P_NUMBER END) as crosby_sev4_030,
COUNT(CASE WHEN Assignment = 'Crosby' AND Severity = 5 AND datediff('day',getdate(), OpenTime between 0 and 30 THEN P_NUMBER END) as crosby_sev5_030,
COUNT(CASE WHEN Assignment = 'Crosby' AND Severity = 4 AND datediff('day',getdate(), OpenTime between 31 and 60 THEN P_NUMBER END) as crosby_sev4_3160,
COUNT(CASE WHEN Assignment = 'Crosby' AND Severity = 5 AND  datediff('day',getdate(), OpenTime between 31 and 30 THEN P_NUMBER END) as crosby_sev5_3160,
COUNT(CASE WHEN Assignment = 'Crosby' AND Severity = 4 AND  datediff('day',getdate(), OpenTime > 60 THEN P_NUMBER END) as crosby_sev4_60,
COUNT(CASE WHEN Assignment = 'Crosby' AND Severity = 5 AND  datediff('day',getdate(), OpenTime > 60 THEN P_NUMBER END) as crosby_sev5_60
FROM dashboard.dbo.SmThings
WHERE Assignment IN('Crosby')
  AND Severity IN(4,5)

答案 2 :(得分:0)

这个怎么样?

WITH Assignments AS (
    SELECT
       Grp = Assignment + '_Sev' + Convert(varchar(11), Severity) + '_' + Label
    FROM
       dashboard.dbo.SmThings
       CROSS APPLY (
          SELECT Age = DateDiff(OpenTime, GetDate())
       ) A
       INNER JOIN (
          SELECT '030', 0, 30, '030'
          UNION ALL SELECT '3160', 31, 59 -- notice 59 here!
          UNION ALL SELECT '60', 60, 2147483647
       ) X (Label, Low, High)
          ON A.Age BETWEEN X.Low AND X.High
    WHERE Assignment IN ('Crosby')
    AND Severity IN (4, 5)
)
SELECT
   Grp,
   Value = Count(*)
INTO #Data
FROM
   Assignments
GROUP BY
   Grp
;

然后,您可以自动使用这些生成的值进行透视(需要SQL Server 2005或更高版本):

DECLARE @SQL nvarchar(max);
SET @SQL = '
SELECT *
FROM
   #Data 
   PIVOT (Max(Value) FOR Grp IN (' 
+ Stuff(
     (SELECT ', ' + Convert(nvarchar(max), QuoteName(Grp)) 
     FROM #Data FOR XML PATH(''), TYPE).value('.[1]', 'nvarchar(max)'),
     1, 2, 0
  ) + ')) P
';

EXEC (@SQL);

观看魔法发生! (假设我没有犯任何错误,这不太可能。)