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

时间:2019-06-23 07:48:20

标签: sql-server reporting-services expression ssrs-2014 ssrs-2017

我正在尝试根据表格记录填充颜色。

从SQL Server 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

第二个,我需要获取如下数据并显示在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”(即“另一个数据集”值)填充第二个结果集中的颜色; 我正在使用的:

       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")

似乎“查找”中的“或”不起作用。我该如何进行这项工作?还有其他方法吗?

第二个结果集中中,想法是:

        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 /空白应该没有任何颜色。

第二个表达式有错误:

            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.

我该如何实现?或者,请让我知道是否还有其他方法可以实现同一目标?谢谢

2 个答案:

答案 0 :(得分:1)

看到您具有从Name0到Name4的静态列。.您可以简单地将每列连接回资源表以获取状态并基于结果。.返回一种颜色,然后将该颜色用于单元格报告。

我将首先用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语句,直到获得所有组合

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

答案 1 :(得分:0)

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

min_v