0

我已经成功地将我的数据转换为数月。但是,数据透视表只返回在我的 KPIData 表中具有值的行。

我正在针对 KPIData 旋转 Months,并从 KPIGraph 中引入一些字段进行过滤。每当我添加 KPIGraphs 行并且 KPIData 中没有相关记录时,我希望能够在每个月列的行中显示空值。目前,这只适用于我在我的 KPIData 表中添加至少 1 个值,然后由于连接而将所有内容链接在一起。

关系设置如下:http: //goo.gl/9lvhF

我做了一些阅读,发现我需要在这里使用交叉连接?我从未使用过其中之一,经过进一步挖掘后,它在我的数据透视 sql 中的实现完全让我感到困惑。

SELECT *
FROM (
    SELECT [t0].[GraphID], CASE MonthID
            WHEN 1 THEN 'October'
            WHEN 2 THEN 'November'
            WHEN 3 THEN 'December'
            WHEN 4 THEN 'January'
            WHEN 5 THEN 'February'
            WHEN 6 THEN 'March'
            WHEN 7 THEN 'April'
            WHEN 8 THEN 'May'
            WHEN 9 THEN 'June'
            WHEN 10 THEN 'July'
            WHEN 11 THEN 'August'
            WHEN 12 THEN 'September'
            END AS [Month], [t0].[Value], [t3].[Graph_Type], [t5].[Department], [t6].
        [Type], [t8].[Financial_Year], [t7].[FYID], [t4].[KPITypeID]
    FROM [KPIDatas] AS [t0]
    INNER JOIN [Months] AS [t1] ON [t1].[ID] = [t0].[MonthID]
    INNER JOIN [KPIGraphs] AS [t2] ON [t2].[ID] = [t0].[GraphID]
    INNER JOIN [GraphTypes] AS [t3] ON [t3].[ID] = [t2].[GraphTypeID]
    INNER JOIN [KPIs] AS [t4] ON [t4].[ID] = [t2].[KPIID]
    INNER JOIN [Departments] AS [t5] ON [t5].[ID] = [t4].[DeptID]
    INNER JOIN [KPITypes] AS [t6] ON [t6].[ID] = [t4].[KPITypeID]
    INNER JOIN [CSFs] AS [t7] ON [t7].[ID] = [t4].[CSFID]
    INNER JOIN [FYs] AS [t8] ON [t8].[ID] = [t7].[FYID]
    ) TableDate
PIVOT(SUM(Value) FOR [Month] IN (
            [October], [November], [December], [January], [February], [March], [April]
            , [May], [June], [July], [August], [September]
            )) PivotTable

对此的任何帮助表示赞赏。

4

1 回答 1

0

在更多的混乱之后设法解决了这个问题。我的子查询需要一个 RIGHT OUTER JOIN 以确保它拉回 Null。然后,当 Pivot 代码开始发挥作用时,这些内容将在我的几个月内展开。

                      dbo.KPIDatas AS t0 RIGHT OUTER JOIN
于 2012-09-21T10:53:33.710 回答