0

以下は、ピボットされた結果を生成するための私のクエリです:

SELECT '# of Corrective Actions open and overdue' as [Corrective Actions breakdown], 
[405],
[2865],
[3142],
[405]+[2865]+[3142] as [Total]
FROM
(Select il.Locationid , ca.CorrectiveActionsid, il.locOrder, ca.isDeleted caDeleted, i.isDeleted iDeleted, i.CompanyId companyid,ca.CADateBy, ca.Status  from IncidentLocation il inner join incident i on il.IncidentId = i.IncidentId 
inner join CorrectiveActions ca on i.IncidentId = ca.IncidentId  
) AS SourceTable
PIVOT
(
COUNT(CorrectiveActionsid)
FOR LocationId IN ([405],[2865],[3142])
) PivotTable 

where locOrder = 0 and caDeleted =0 and iDeleted = 0 and companyId = 210
and CADateBy <= '2013-01-01'  and [Status] = 'Open'

空白のデータの場合、カウントは o を返す必要があると考えていました。しかし、私はまったく結果を得ていません。私が何をしているのか、そしてすべてのカウントされた値を空白にするのではなくゼロにするために何をすべきかを教えてください。

4

3 に答える 3

0

SQL Server では、COUNT は NULL 値を無視します。そうは言っても、集計する列で ISNULL 関数を使用してください。

   SELECT '# of Corrective Actions open and overdue' as [Corrective Actions breakdown], 
        [405],
        [2865],
        [3142],
        [405]+[2865]+[3142] as [Total]
   FROM
        (Select il.Locationid , ISNULL(ca.CorrectiveActionsid, 0) AS CorrectiveActionsid, il.locOrder, ca.isDeleted caDeleted, i.isDeleted iDeleted, i.CompanyId companyid,ca.CADateBy, ca.Status  from IncidentLocation il inner join incident i on il.IncidentId = i.IncidentId 
        inner join CorrectiveActions ca on i.IncidentId = ca.IncidentId  
        ) AS SourceTable
   PIVOT
        (
        COUNT(CorrectiveActionsid)
        FOR LocationId IN ([405],[2865],[3142])
        ) PivotTable 

   where locOrder = 0 and caDeleted =0 and iDeleted = 0 and companyId = 210
        and CADateBy <= '2013-01-01'  and [Status] = 'Open'
于 2013-09-19T03:32:12.560 に答える