根据 SSRS 中其他数据集的值填充颜色

Fill color based on values from other data set in SSRS

我正在尝试根据 table 条记录填充颜色。

来自 SQL 服务器 AdventureWorks2017 数据库:

          select * from [HumanResources].[Department]

我有两个数据集:

           1st : select * from [HumanResources].[Department]

            DepartmentID                         Name                            GroupName                                          Status
            ----------------------------------------------------------------------------------------------------------------------------------------
            1                                Engineering                    Research and Development                       Cancelled
            2                                Tool Design                    Research and Development                       Blacklist
            3                                Sales                          Sales and Marketing                            Approved
            4                                Marketing                      Sales and Marketing                            All good
            5                                Purchasing                     Inventory Management                           xxx
            6                                Research and Development       Research and Development                       yyy
            7                                Production                     Manufacturing                                  zzz
            8                                Production                     Control Manufacturing                          xxx
            9                                Human Resources                Executive General and Administration           zzz
            10                               Finance                        Executive General and Administration           aaa
            11                               Information Services           Executive General and Administration           xxx
            12                               Document Control               Quality Assurance                              yyy
            13                               Quality Assurance              Quality Assurance                              zzz
            14                               Facilities and Maintenance     Executive General and Administration           aaa
            15                               Shipping and Receiving         Inventory Management                           bbb
            16                               Executive                      Executive General and Administration           aaa

第2次,我有一个需求是获取如下数据并显示在SSRS报告中:

                Name0                                Name1                                      Name2                           Name3                                      Name4
            -----------------------------------------------------------------------------------------------------------------------------------------------------------------
            Document Control           Facilities and Maintenance                Information Services           Production Control                  Research and Development
             Engineering                Finance                                     Marketing                      Purchasing                       Sales
             Executive                 Human Resources                             Production                   Quality Assurance                   Shipping and Receiving
              NULL                     NULL                                         NULL                         NULL                               Tool Design

我使用以下查询获取的上述数据:

            WITH Table1 AS
            (
                SELECT  [Name]
                FROM    [HumanResources].[Department]
            ), CTE AS
            (
                SELECT  [Name], COL, ROW_NUMBER() OVER(PARTITION BY Col ORDER BY [Name]) AS Row
                FROM    (   SELECT [Name],
                                    5 - NTILE(5) OVER(ORDER BY [Name] DESC) AS Col
                            FROM Table1  
                        ) c
            )
            SELECT [0], [1], [2], [3], [4]
            FROM CTE 
            PIVOT (MAX([Name]) FOR Col IN ([0], [1], [2], [3], [4])) AS Pvt
            ORDER BY Row;

另外,除了上述要求外,还有一个要求是根据"GroupName"和"Status"在第2个结果集中填充颜色是 "another dataset" 值; 我正在使用 :

       select * from [HumanResources].[Department]

我正在尝试以下表达式,以填充第二个数据集的颜色:

      =IIF(LOOKUP((Fields!ID0.Value or Fields!ID1.Value or Fields!ID2.Value or Fields!ID3.Value or Fields!ID4.Value),Fields!Name.Value,Fields!GroupName.Value="Research and Development","DsetAll"),"GREEN","WHITE") 

      =IIF(LOOKUP((Fields!ID0.Value or Fields!ID1.Value or Fields!ID2.Value or Fields!ID3.Value or Fields!ID4.Value),Fields!Name.Value,Fields!GroupName.Value,"DsetAll")="Research and Development","GREEN","WHITE")

问题是以上表达式的 NON 工作正常。

编辑: 下面的表达式看起来工作正常:

 =IIF(LOOKUP(Fields!ID0.Value ,Fields!Name.Value,Fields!GroupName.Value,"DsetAll")="Research and Development","BLUE","RED")

看起来,"OR" 在 "lookup" 中不起作用。我怎样才能使这项工作?还有其他方法吗?

第2个结果集中,思路是:

        if groupname "Research and Development" and status is "Cancelled" then "Green"
       if groupname "Research and Development" and status is "Blacklist" then "Green"
       if groupname "Sales and Marketing" and status is "Approved" then "Green"
                if status is "All good" then Gray
                if groupname "Manufacturing" then Blue
                if groupname "Control   Manufacturing" then red
                if groupname "Executive General and Administration" then pink
                if groupname "Quality Assurance" then Violet
                Else its should be white

第一个表达式没有显示任何填充颜色。 此外,NULL/Blank 不应有任何颜色。

第二个表达式有错误:

            The Error occurred during local report processing.
            The definition of the report '/ColorTest' is invalid.
            The BackgroundColor expression for the text box 'ID0' contains an error: [BC30201] Expression expected.

我怎样才能做到这一点?或者,请让我知道是否有任何其他方法可以实现同样的目标?谢谢

这样表达式应该可以工作,但我建议使用另一种方法(可能使用计算字段将特定信息存储在一个字段而不是五个单独的 ID 字段中):

=IIF(LOOKUP(Fields!ID0.Value, Fields!Name.Value, Fields!GroupName.Value, "DsetAll") = "Research and Development" Or
     LOOKUP(Fields!ID1.Value, Fields!Name.Value, Fields!GroupName.Value, "DsetAll") = "Research and Development" Or
     LOOKUP(Fields!ID2.Value, Fields!Name.Value, Fields!GroupName.Value, "DsetAll") = "Research and Development" Or
     LOOKUP(Fields!ID3.Value, Fields!Name.Value, Fields!GroupName.Value, "DsetAll") = "Research and Development" Or
     LOOKUP(Fields!ID4.Value, Fields!Name.Value, Fields!GroupName.Value, "DsetAll") = "Research and Development" 
    ,"BLUE"
    ,"RED")

看到您将 Name0 到 Name4 作为静态列..您可以简单地将每一列连接回资源 table 以获取状态并根据结果.. return a颜色,然后将该颜色用于报告中的单元格。

我会通过首先用 cte 包装你的 Pivot 并获得整洁的输出来解决这个问题。

类似

    , my_pivot_result as (

     SELECT [0], [1], [2], [3], [4]
                FROM CTE 
                PIVOT (MAX([Name]) FOR Col IN ([0], [1], [2], [3], [4])) AS Pvt

    )
,my_status as (

select my_pivot_result .* 
, isnull(col1_name.status,'') as col1_status
, isnull(col1_name.groupname,'') as col1_groupname
, isnull(col2_name.status,'') as col2_status
, isnull(col2_name.groupname,'') as col2_groupname
, isnull(col3_name.status,'') as col3_status
, isnull(col3_name.groupname,'') as col3_groupname
, isnull(col4_name.status,'') as col_status
, isnull(col4_name.groupname,'') as col4_groupname

from my_pivot_result 

left join [HumanResources].[Department] col1_name
 on col1_name.name = my_pivot_result.name0

left join [HumanResources].[Department] col2_name
 on col2_name.name = my_pivot_result.name1

left join [HumanResources].[Department] col3_name
 on col3_name.name = my_pivot_result.name2

left join [HumanResources].[Department] col4_name
 on col4_name.name = my_pivot_result.name3
)

Select * from my_status

现在对于每一行,您应该有一个状态和组名。现在使用 case 语句为颜色构建另一个列。

有点像..

    case when  col1_groupname = 'Research and Development'
       and col1_status in('Blacklist', 'Cancelled') then 'Green'

 else 'white' end as col1_color

    .
    .
    .

继续构建 case 语句,直到拥有所有组合

现在在前端,对每个单元格使用一个表达式来显示颜色并使用颜色字段值..