如何计算列中行组的结果?

时间:2014-11-06 17:20:39

标签: reporting-services ssrs-2008 rows

我的表格如下:

 Customer         Error1         Error2         Error3         Error4       TotalErrors      TotalOrders
 CustomerName [SUM(Error1)]   [SUM(Error2)]  [SUM(Error3)] [SUM(Error3) [SUM(TotalErrors)]  [TotalOrders]
 Total

中间行CustomerName是在CustomerWithErrors上排序的行组的一部分,它只是告诉给定的客户名称是否包含4个错误中的任何一个。如果没有,它就不会出现在表格中。

在最后一行中,我只需右键单击并为行组中的每列添加总计。除了最后一列之外,它会按照我的要求输出每列的总和。当我添加总计时,它会为我提供所有客户名称的TotalErrors字段的总和,而不仅仅是有错误的字段(实际显示在表中的那些)。因此,数量巨大。我怎样才能得到总误差列中显示的数字总和?

编辑:以下是设计和预览模式的一些截图:

设计:http://i.imgur.com/6kEmzw3.png

预览:http://i.imgur.com/NOpWmAI.png

编辑2:SQL for TOP TABLIX:

其中一些对我来说是新的,所以它可能看起来不太好。让我知道我能解释什么。感谢。

 SELECT 
         PKEY
        ,Customer
        ,isnull(CustomerName, 'Unknown') AS CustomerName
        ,CustomerWithErrors
        ,Warehouse
        ,ErrorMessage
        ,PROCESSDATE
        ,CARTONERRORMESSAGE
        ,Division
        ,BOLNO
        ,BOLSHIPDATE
        ,CARTONDETAILERRMSG
        ,ErrorCategory
        ,CASE WHEN ErrorCategory='Success'
        THEN 0
        ELSE    
        COUNT(NotLate) 
        END AS OnTimeCount
        ,COUNT(Invalid) AS InvalidCount
        ,COUNT(WrongData) AS WrongDataCount
        ,COUNT(Duplicate) AS DuplicateCount
        ,COUNT(MissingData) AS MissingDataCount
        ,(COUNT(Invalid)+COUNT(WrongData)+COUNT(Duplicate)+COUNT(MissingData)) AS TotalErrors
        ,Total945s AS TotalReport945s
        --,COUNT(*) OVER(PARTITION BY [CustomerWithErrors]) Total945s 
        , (SELECT (Total945s) WHERE CustomerWithErrors>=0) AS TotalErrors

        FROM(

    SELECT
         PKEY
        ,Customer
        ,CASE WHEN CustomerName = '' OR CustomerName IS NULL
        THEN 'Unknown'
        ELSE CustomerName
        END AS CustomerName
        ,CASE WHEN CustomerName='' OR CustomerName IS NULL OR CustomerName='Unknown'
        THEN 0
        ELSE
        (SELECT DISTINCT Customer WHERE ErrorCategory <> 'Success') END
         AS CustomerWithErrors
        ,Warehouse
        ,ErrorMessage
        ,PROCESSDATE
        ,CARTONERRORMESSAGE
        ,Division
        ,BOLNO
        ,BOLSHIPDATE
        ,CARTONDETAILERRMSG
        ,ErrorCategory
        ,CASE WHEN ErrorCategory='Success'
        THEN 'N'
        ELSE OnTime         
        END AS OnTime
        ,(SELECT Customer WHERE OnTime = 'Y') AS NotLate
        ,(SELECT Customer WHERE ErrorCategory = 'Invalid') AS Invalid
        ,(SELECT Customer WHERE ErrorCategory = 'Wrong Data') AS WrongData
        ,(SELECT Customer WHERE ErrorCategory = 'Duplicate') AS Duplicate
        ,(SELECT Customer WHERE ErrorCategory = 'Missing Data') AS MissingData
        , COUNT(*) OVER(PARTITION BY [Customer]) Total945s



    FROM EDI945UTIErrors


    ) S


        GROUP BY
         PKEY
        ,Customer
        ,CustomerName
        ,CustomerWithErrors
        ,Warehouse
        ,ErrorMessage
        ,PROCESSDATE
        ,CARTONERRORMESSAGE
        ,Division
        ,BOLNO
        ,BOLSHIPDATE
        ,CARTONDETAILERRMSG
        ,ErrorCategory
        ,OnTime
        ,Total945s

编辑3:SQL with PIVOT: 这看起来像我应该在报告中使用聚合吗?感谢。

 SELECT 
         S.PKEY 
        ,Customer
        ,isnull(S.CustomerName, 'Unknown') AS CustomerName
        ,CustomerWithErrors
        ,Warehouse
        ,ErrorMessage
        ,PROCESSDATE
        ,CARTONERRORMESSAGE
        ,Division
        ,BOLNO
        ,BOLSHIPDATE
        ,CARTONDETAILERRMSG
        ,ErrorCategory
        ,CASE WHEN ErrorCategory='Success'
        THEN 0
        ELSE    
        COUNT(NotLate) 
        END AS OnTimeCount
        ,Invalid
        ,WrongData
        ,Duplicate
        ,MissingData
        --,COUNT(Invalid) AS InvalidCount
        --,COUNT(WrongData) AS WrongDataCount
        --,COUNT(Duplicate) AS DuplicateCount
        --,COUNT(MissingData) AS MissingDataCount
        --,(COUNT(Invalid)+COUNT(WrongData)+COUNT(Duplicate)+COUNT(MissingData)) AS TotalErrors
        ,Total945s AS TotalReport945s

        --,COUNT(*) OVER(PARTITION BY [CustomerWithErrors]) Total945s 
        --, (SELECT (Total945s) WHERE CustomerWithErrors>=0) AS TotalErrors

        FROM(

    SELECT
         PKEY 
        ,Customer
        ,CASE WHEN CustomerName = '' OR CustomerName IS NULL
        THEN 'Unknown'
        ELSE CustomerName
        END AS CustomerName
        ,CASE WHEN CustomerName='' OR CustomerName IS NULL OR CustomerName='Unknown'
        THEN 0
        ELSE
        (SELECT DISTINCT Customer WHERE ErrorCategory <> 'Success') END
         AS CustomerWithErrors
        ,Warehouse
        ,ErrorMessage
        ,PROCESSDATE
        ,CARTONERRORMESSAGE
        ,Division
        ,BOLNO
        ,BOLSHIPDATE
        ,CARTONDETAILERRMSG
        ,ErrorCategory
        ,CASE WHEN ErrorCategory='Success'
        THEN 'N'
        ELSE OnTime         
        END AS OnTime
        ,(SELECT Customer WHERE OnTime = 'Y') AS NotLate
        --,(SELECT Customer WHERE ErrorCategory = 'Invalid') AS Invalid
        --,(SELECT Customer WHERE ErrorCategory = 'Wrong Data') AS WrongData
        --,(SELECT Customer WHERE ErrorCategory = 'Duplicate') AS Duplicate
        --,(SELECT Customer WHERE ErrorCategory = 'Missing Data') AS MissingData
        , COUNT(*) OVER(PARTITION BY [Customer]) Total945s



    FROM EDI945UTIErrors
    --WHERE Customer ='104646' and OnTime = 'Y'
    --WHERE CUSTOMER='107834'
    --AND ErrorCategory = 'Wrong Data'
    --AND ERRORMESSAGE LIKE '%Overshipment Not Allowed%'

    ) S
    LEFT JOIN
    (
    SELECT PKEY, ISNULL(CustomerName, 'Unknown') AS CustomerName, [Invalid], [WrongData], [Duplicate], [MissingData], [Success]
 FROM EDI945UTIErrors
 PIVOT (Count(Customer) FOR ErrorCategory in ([Invalid], [WrongData], [Duplicate], [MissingData], [Success]))
 AS P) AS PT ON S.PKEY = PT.PKEY

        GROUP BY
         S.PKEY
        ,Customer
        ,S.CustomerName
        ,CustomerWithErrors
        ,Warehouse
        ,ErrorMessage
        ,PROCESSDATE
        ,CARTONERRORMESSAGE
        ,Division
        ,BOLNO
        ,BOLSHIPDATE
        ,CARTONDETAILERRMSG
        ,ErrorCategory
        ,OnTime
        ,Total945s
        ,Invalid
        ,WrongData
        ,MissingData
        ,Duplicate

非常感谢你的时间。

1 个答案:

答案 0 :(得分:1)

根据您发布的图片,您的数据集可能会为客户提供所有订单,而不仅仅是那些有错误的订单。检查此问题的一种好方法是从数据集中获取SQL并在Management Studio中运行它。如果您获得了不想计算的行,则可以在Tablix上使用过滤器。

要在Tablix上应用过滤器,请右键单击它并转到Tablix属性: TablixProperties

点击过滤器,你可以选择只有错误标志的订单: TablixFilter

如果这不能解决您的问题,请告诉我并将数据集的SQL添加到您的问题中,我们将返回绘图板。 :)

编辑 - 2014年12月11日

感谢SQL!如果CustomerWithErrors仅返回客户编号,则过滤器将无法帮助。你还在拉每条记录(甚至是那些ErrorCategory ='成功'的记录)。您是否知道有两​​列别名为TotalErrors?有

(COUNT(Invalid)+COUNT(WrongData)+COUNT(Duplicate)+COUNT(MissingData)) AS TotalErrors

(SELECT (Total945s) WHERE CustomerWithErrors>=0) AS TotalErrors

第一个看起来可能有用,但我不知道你在桌子上使用了哪个。另一件需要考虑的事情是,您正在计算聚合的聚合。如果遇到结果集中存在这种情况的情况,最终可能会将它们相乘:

Customer    |   ErrorCategory   |   TotalErrors
1234        |   Invalid         |   2
1234        |   Duplicate       |   2

您将在报告中看到的结果:

Customer    |   TotalErrors
1234        |   4

如果您想避免这种情况,可以让SSRS进行聚合。你考虑过做一个PIVOT吗? 你可以这样做:

Select Customer, ISNULL(CustomerName, 'Unknown'), [Invalid], [Wrong Data], [Duplicate], [Missing Data], [Success]
FROM EDITable
PIVOT (Count(Customer) FOR ErrorCategory in ([Invalid], [Wrong Data], [Duplicate], [Missing Data], [Success]))

然后在报告中输入总错误,使用表达Sum(无效)+ Sum(错误数据)+ Etc的表达式。只要每个客户有一条记录,您的数据就不应该加倍,您仍然可以获得非错误记录的总数。

我希望有所帮助。如果您有任何疑问,请与我联系!