Populating with '0' when Data in SSRS Does not exist

后端 未结 2 2029
没有蜡笔的小新
没有蜡笔的小新 2021-01-07 12:02

I\'m trying to create a report in SSRS where I have a matrix, which has gender as the column headings and specifically defined agegroups as the rows. The report is sorted by

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-07 12:33

    I know this is old, but I wanted to elaborate on Ian's section 1 above using joins at the dataset level. (His answer was super helpful to me for a report I'm working on.)

    per op:

    Is the solution to have a separate dataset that brings back the entire list of categories and then somehow fit them together?

    That is how I've handled it successfully, but you can do so without actually creating a separate dataset by using common table expressions (or temp tables, of course).

    For these example tables:

           AGE_Table
        ID   Group     Group_Desc    Toys
        1    A         00-10        Teddy Bear
        2    B         11-20        Video Game
        3    C         21-30        Sports Car
        4    D         31-40        Mansion
        5    E         41-50        Jewelry
    
                     People_Table  (filtered for whatever date)
        ID    Name          Age    Gender   Age_Group
        1     Ariel         07     F        A
        2     Brandon       23     M        C
        3     Chelsea       27     F        C
        4     Derek         06     M        A
    

    You want to see 2 results for the 00-10 row, 2 for the 21-30 row, and then still see rows for the other age groups even if there aren't any results.

    We want to create a dataset with all the different age groupings and then join on it. Behold a solution using common table expressions:

        with CTE_Age AS
        (SELECT Distinct Age_Group from AGE_Table)
    
        SELECT ID, Name, Age, Gender, CTE_Age.Age_Group FROM People_Table
        RIGHT JOIN CTE_Age ON
        People_Table.Age_Group = CTE_Age.Age_Group
    

    This will return:

        ID     Name        Age     Gender     Age_Group
        1      Ariel       7       F          A
        4      Derek       6       M          A
        NULL   NULL        NULL    NULL       B
        2      Brandon     23      M          C
        3      Chelsea     27      F          C
        NULL   NULL        NULL    NULL       D
        NULL   NULL        NULL    NULL       E
    

    Once you have that in your dataset, you can change NULL values to 0 on the report builder side -- I think in 2008R2 the default is just blank.

提交回复
热议问题