如果当月没有数据,则显示0表示累积计数

时间:2016-01-27 05:45:20

标签: sql-server

我有一个包含网站,月份,预计计数和实际计数的sql表。投影计数存储为 来自我的客户的累积价值。实际计数是另一个表的派生值。

以下是表格数据的屏幕截图。

enter image description here

我想找到每个月和网站的实际Cnt数据的累积计数。我是基于这样做的 以下查询及其正常工作。

SELECT DISTINCT
[SiteID] ,
ActualCount ,
CONVERT(DATETIME, [Month], 106) AS [Month] ,
EventName ,
EventValue
FROM ( SELECT DISTINCT
[SiteID] ,
ActualCount ,
[Month] ,
CASE WHEN [ActualCount] IS NULL
THEN ISNULL(SUM([ActualCount]) OVER ( PARTITION BY [SiteID]
ORDER BY CONVERT(DATETIME, [Month], 106) ),
0)
ELSE ISNULL(SUM([ActualCount]) OVER ( PARTITION BY [SiteID]
ORDER BY CONVERT(DATETIME, [Month], 106) ),
0)
END AS CumActSum ,
ISNULL(SUM([ProjectedCount]) OVER ( PARTITION BY [Month],
[SiteID] ORDER BY [Month]
),
0) AS CumProSum
FROM [db_Temp].[dbo].[tbl_site_projection]
) AS src UNPIVOT ( EventValue FOR EventName IN ( CumActSum, CumProSum ) ) AS
UPV

上述代码的输出是

enter image description here

问题是我的客户的要求。如果您查看数据,那么该月没有实际计数 所有siteid的12月份都是NULL

根据我的客户要求,如果一个月内所有siteid都没有实际计数,那么累积计数 那个月必须为零。我们不得显示该月的累积计数。

我尝试通过考虑像WHEN ActualCount IS NULL THEN 0这样的条件来修改代码。但这会删除 11月份的累计数也。

基本上他们想要的是,只要所有站点的实际计数为空,就不能显示累积计数 那个月,我们必须显示0。

以下是我要找的预期产量。

enter image description here

以下是SQL Fiddle

sql fiddle中存在一些问题,因此我刚刚构建了该模式。

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

更改您的案例陈述,以检查当月的实际计数的累计总和是否为空,如果为零或0,则将其设为0。

SELECT DISTINCT
        [SiteID] ,
        ActualCount ,
        CONVERT(DATETIME, [Month], 106) AS [Month] ,
        EventName ,
        EventValue
FROM    ( SELECT DISTINCT
                    [SiteID] ,
                    ActualCount ,
                    [Month] ,
                    CASE WHEN SUM([ActualCount]) OVER ( PARTITION BY [Month]) IS NULL
                         THEN 0
                         ELSE ISNULL(SUM([ActualCount]) OVER ( PARTITION BY [SiteID] ORDER BY CONVERT(DATETIME, [Month], 106) ),
                                     0)
                    END AS CumActSum ,
                    ISNULL(SUM([ProjectedCount]) OVER ( PARTITION BY [Month],
                                                        [SiteID] ORDER BY [Month] ),
                           0) AS CumProSum 
          FROM      [db_Temp].[dbo].[tbl_site_projection] GROUP BY [SiteID] ,
                    ActualCount ,
                    [Month], [ProjectedCount] 
        ) AS src UNPIVOT ( EventValue FOR EventName IN ( CumActSum, CumProSum ) ) AS UPV
        WHERE TrialMonth LIKE 'Dec%' AND EventName = 'CumActSum'
        ORDER BY CONVERT(DATETIME, [Month], 106), SiteID

答案 1 :(得分:0)

为什么你不能直接处理null呢?

String image_name = "image1.png";

ImageView imageView = (ImageView)view.findViewById(R.id.myImageView);
imageView.setImageResource(image_name); // FAIL since is expecting the drawable and not the name as a String.

其他明智地在内部查询本身进行修改,如下所示,我认为不需要案例条件。您在总和之后处理null。在sum之前尝试null处理。它也可以避免一些严重的问题。

    SELECT DISTINCT
    [SiteID] ,
    ISNULL(ActualCount ,0),  --Here I handle NULL
    CONVERT(DATETIME, [Month], 106) AS [Month] ,
    EventName ,
    EventValue
    FROM ( SELECT DISTINCT
    [SiteID] ,
    ActualCount ,
    [Month] ,
    CASE WHEN [ActualCount] IS NULL
    THEN ISNULL(SUM([ActualCount]) OVER ( PARTITION BY [SiteID]
    ORDER BY CONVERT(DATETIME, [Month], 106) ),
    0)
    ELSE ISNULL(SUM([ActualCount]) OVER ( PARTITION BY [SiteID]
    ORDER BY CONVERT(DATETIME, [Month], 106) ),
    0)
    END AS CumActSum ,
    ISNULL(SUM([ProjectedCount]) OVER ( PARTITION BY [Month],
    [SiteID] ORDER BY [Month]
    ),
    0) AS CumProSum
    FROM [db_Temp].[dbo].[tbl_site_projection]
    ) AS src UNPIVOT ( EventValue FOR EventName IN ( CumActSum, CumProSum ) ) AS
    UPV