SSRS - 矩阵表读取上一行值

时间:2018-03-20 20:27:05

标签: vb.net reporting-services report ssrs-2012

我一直试图在矩阵表上读取前一行值(显示每小时数量),将其与当前行值进行比较,如果它们之间的差异超出可接受的容差范围,则将背景颜色突出显示为黄色范围。

矩阵表具有图片上显示的列和行分组(仅用于每小时信息)。

尝试使用自定义代码来实现此目的,但我得到的结果不正确。

我已附上报告设计图片及其结果以及客户代码。

请帮我弄清楚逻辑上有什么问题。

图片: Design结果: Design and Result 代码:在报告属性下编写的自定义代码 - >代码

Public Shared previous as Object 
Public Shared current as Object = Nothing

Public Shared Function GetCurrent(Item as Object) as Object
    previous=current
    current=Item
    return current
End Function

Public Shared Function GetPrevious()
    return previous
End Function


public Shared Function IsHourlyDiffAboveTolerance(previousValue as Integer, currentValue as Integer, variantPercent as Decimal, variantVolume as Decimal, sortOrder as Integer) As Object

    If previousValue = Nothing Then
        Return Nothing
    Else
        Dim diff as Integer
        diff = abs(currentValue - previousValue)

        Dim tolerance as decimal
        tolerance = 0.0

        If variantPercent <> 0 Then
            tolerance = previousValue * (variantPercent/100)
        Else If variantVolume <> 0 Then
            tolerance = variantVolume
        End If

        If diff >= 0 And diff <= tolerance Then
            return Nothing
        Else
            return 1
        End If
    End If
End Function

来电者表达:

=IIF(
IsNothing(Code.IsHourlyDiffAboveTolerance(Code.GetPrevious(), Code.GetCurrent(Fields!ExpectedQty.Value), 
    Fields!VariantPercent.Value, Fields!VariantVolume.Value, Fields!SortOrder.Value)), 
"No Color", 
"Yellow")

更新:可以存在变体量或数量,但不能同时存在。

由于

1 个答案:

答案 0 :(得分:1)

我会为SQL Server 2012使用新的分析函数LEAD()LAG()。这些函数在同一结果集中访问后续行(针对潜在客户)和上一行(针对延迟)的数据没有使用自我加入。要将这些函数与矩阵一起使用,您可能需要在子句中使用PARTITION BY进行分组。

示例SQL:

USE AdventureWorks
GO

SELECT 
      s.SalesOrderID
    , s.SalesOrderDetailID,s.OrderQty
    , LeadValue = LEAD(SalesOrderDetailID) OVER (ORDER BY SalesOrderDetailID) 
    , LagValue = LAG(SalesOrderDetailID) OVER (ORDER BY SalesOrderDetailID) 
FROM 
    Sales.SalesOrderDetail s
WHERE 
    SalesOrderID IN (43670, 43669, 43667, 43663)
ORDER BY 
      s.SalesOrderID
    , s.SalesOrderDetailID
    , s.OrderQty
GO

enter image description here

Reference Article