Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于空白数据的SQL数据透视表_Sql_Sql Server_Sql Server 2008_Pivot - Fatal编程技术网

用于空白数据的SQL数据透视表

用于空白数据的SQL数据透视表,sql,sql-server,sql-server-2008,pivot,Sql,Sql Server,Sql Server 2008,Pivot,以下是生成数据透视结果的查询: 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.is

以下是生成数据透视结果的查询:

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。但我没有得到任何结果。请告诉我我做错了什么,我应该怎么做才能使所有计数值都为零而不是为空。

我希望计数返回0。如果没有,您可以尝试使用coalesce:


在SQL Server中,计数将忽略空值。也就是说,对要聚合的列使用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'

看起来您的查询没有返回任何行,您可以这样修复它我没有修复查询的其他部分,只是想向您展示一个解决方法:

select
    A.[Corrective Actions breakdown],
    coalesce([405], 0) as [405],
    coalesce([2865], 0) as [2865],
    coalesce([3142], 0) as [3142],
    coalesce([405], 0) + coalesce([2865], 0) + coalesce([3142], 0) as [Total]
from (select '# of Corrective Actions open and overdue' as [Corrective Actions breakdown]) as A
    outer apply (
   SELECT
        [405],
        [2865],
        [3142]
   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'
    ) as p

CorrectiveActionsid附近的语法不正确。期待,或选择。我刚刚编辑了我的答案。我将ISNULL移动到SourceTable子查询中的SELECT。试试看。我基本上只是复制了你的查询并添加了ISNULL。如果您将问题中的查询复制到SSM并运行,是否会出现错误?否,对于将isnull移动到SourceTable query的查询,我没有发现任何错误。只是产生相同的空白结果。你说的空白结果是什么意思?没有结果?无效的空字符串?你能简单地告诉我你到底想完成什么吗?我的语法不正确,几乎出错。是的,当然它不返回任何行,但如果SourceTable中没有数据,我尝试获取的是counted value为零事件。其想法是简单地选择一行并命名,将数据加入其中,如果某些计数为nullNope,则使用coalesce。仍然返回0行。确定,将此更改为外部应用请参阅更新的或左侧外部联接。。。在1=1时,它现在可以工作。谢谢:
select
    A.[Corrective Actions breakdown],
    coalesce([405], 0) as [405],
    coalesce([2865], 0) as [2865],
    coalesce([3142], 0) as [3142],
    coalesce([405], 0) + coalesce([2865], 0) + coalesce([3142], 0) as [Total]
from (select '# of Corrective Actions open and overdue' as [Corrective Actions breakdown]) as A
    outer apply (
   SELECT
        [405],
        [2865],
        [3142]
   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'
    ) as p