-3

次のクエリを SQL Server 2008 で機能させようとしています。「ピボットテーブル付近の構文が正しくありません」というエラー メッセージ (最後に表示されます) が返されます。誰でもエラーを見つけるのを手伝ってもらえますか?

SELECT * FROM (
 (SELECT p_id, [InfoSec].[dbo].[tb_incidents_secureworks].[classification],
  DATENAME(mm, occurrence) as [month]
  FROM [InfoSec].[dbo].[tb_incidents_secureworks]
  WHERE [InfoSec].[dbo].[tb_incidents_secureworks].[type] = 'Security Incident'
  AND occurrence >= DATEADD(mm, DATEDIFF(mm,0,DATEADD(mm,-12,getdate())), 0)
  AND occurrence < DATEADD(mm, DATEDIFF(mm,0,DATEADD(mm,0,getdate())), 0)
 UNION ALL
 SELECT p_id, [InfoSec].[dbo].[tb_incidents].[classification],
   DATENAME(mm, occurrence) as [month]
   FROM [InfoSec].[dbo].[tb_incidents]
   WHERE (occurrence >= DATEADD(mm, DATEDIFF(mm,0,DATEADD(mm,-12,getdate())), 0)
   AND occurrence < DATEADD(mm, DATEDIFF(mm,0,DATEADD(mm,0,getdate())), 0)))
 AS SourceTable

PIVOT (
 COUNT(p_id)
 FOR [month] in ([August],[September],[October],[November],[December],[January],[February],[March],[April],[May],[June],[July])
) AS PivotTable
4

2 に答える 2

2

私がいつも言っているように、フォーマットは問題を示すかもしれません...

SELECT  * 
FROM    (
            SELECT  p_id, 
                    [InfoSec].[dbo].[tb_incidents_secureworks].[classification],
                    DATENAME(mm, occurrence) as [month]
            FROM    [InfoSec].[dbo].[tb_incidents_secureworks]
            WHERE   [InfoSec].[dbo].[tb_incidents_secureworks].[type] = 'Security Incident'
                    AND occurrence >= DATEADD(mm, DATEDIFF(mm,0,DATEADD(mm,-12,getdate())), 0)
                    AND occurrence < DATEADD(mm, DATEDIFF(mm,0,DATEADD(mm,0,getdate())), 0)
            UNION   ALL
            SELECT  p_id, 
                    [InfoSec].[dbo].[tb_incidents].[classification],
                    DATENAME(mm, occurrence) as [month]
            FROM    [InfoSec].[dbo].[tb_incidents]
            WHERE   (
                        occurrence >= DATEADD(mm, DATEDIFF(mm,0,DATEADD(mm,-12,getdate())), 0)
                        AND occurrence < DATEADD(mm, DATEDIFF(mm,0,DATEADD(mm,0,getdate())), 0))
                    )
        ) AS SourceTable
PIVOT 
(
    COUNT(p_id)
    FOR [month] in ([August],[September],[October],[November],[December],[January],[February],[March],[April],[May],[June],[July])
) AS PivotTable
于 2013-08-09T15:39:23.373 に答える