-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 回答
0

请像这样尝试:

SELECT Columns 
 FROM (
   SELECT columns
    FROM tbl1
   UNION ALL
   SELECT columns
    FROM tbl2
   )
PIVOT 
于 2013-08-09T15:41:10.470 回答