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

これはどう?

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 に答える
0

MSSQL を使用している場合 - DATEDIFFを使用

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

出力は次のようになります

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 に答える