0

在下面的代码中,我需要使用 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)
4

3 回答 3

0

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

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

输出将是

差异日期
61

于 2013-06-13T17:23:35.020 回答
0

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

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)
于 2013-06-13T17:27:40.250 回答
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);

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

于 2013-06-13T20:28:29.837 回答