查询驱动的SSRS矩阵行和列

时间:2013-02-12 00:34:05

标签: reporting-services matrix group-by

有没有办法创建一个数据表/矩阵,其中行和&列组是从单独的查询/数据集驱动而不是主报表数据?

例如,使用列的“Person”构造: - 名称 - 性别 - marital_status

我希望矩阵的列始终包含所有“性别”,并且行总是包含所有“婚姻状态”,无论查询的条件如何,行/列的交集都是记录。

例如,查询可能是select * from person where name = 'aaron'

返回的所有记录都是“男性”,但我仍然想要包含“女性”列(其中“女性”列中的所有计数都为0)

我希望输出看起来像:

Marital Status:  ~ Male ~ Female
Single ~ 5 ~ 0
Married ~ 8 ~ 0
Defacto ~ 2 ~ 0
...

我不想做虚拟查询,例如:

select 'male' as gender, null as name, null as marital_status
union all 
select 'female' as gender, null as name, null as marital_status
union all
select * from person where [ ...  criteria]

如果可能的话,有3个数据集驱动矩阵会很好......

  • “RowData”包含select distinct marital_status from person
  • “ColumnData”包含select distinct gender from person
  • “MainData”包含`select * from person [... criteria]

有没有办法让Matrix控件对行/列组使用单独的查询?

1 个答案:

答案 0 :(得分:4)

为什么这么难?想想更容易...... 在SQL中创建查询:

WITH
Gender AS
(
    SELECT 1 AS GenderID, 'Male' AS GenderName
    UNION ALL
    SELECT 2 AS GenderID, 'Female' AS GenderName
),
MartialStatus AS
(
    SELECT 1 AS MartialStatusID, 'Single' AS MStatus
    UNION
    SELECT 2 AS MartialStatusID, 'Married' AS MStatus
),
Persons AS
(
    SELECT 1 AS PersonID, 'John' AS Name, 1 AS GenderID, 2 AS MartialStatusID
    UNION ALL
    SELECT 2 AS PersonID, 'Linda' AS Name, 2 AS GenderID, 1 AS MartialStatusID
    UNION ALL
    SELECT 3 AS PersonID, 'Mike' AS Name, 1 AS GenderID, 1 AS MartialStatusID
    UNION ALL
    SELECT 4 AS PersonID, 'Jenna' AS Name, 2 AS GenderID, 1 AS MartialStatusID  
)
SELECT Gender.GenderName, MartialStatus.MStatus, Persons.PersonID, Persons.Name
FROM Gender
CROSS JOIN MartialStatus
LEFT JOIN Persons ON Persons.GenderID = Gender.GenderID AND 
                     Persons.MartialStatusID = MartialStatus.MartialStatusID

产生结果:

Male    Single   3      Mike
Female  Single   2      Linda
Female  Single   4      Jenna
Male    Married  1      John
Female  Married  NULL    NULL

然后在矩阵映射列和行中的BIDS中: enter image description here

如果我们没有人,则此处的表达式为=IIF(Sum(Fields!PersonID.Value) IS Nothing, 0, Sum(Fields!PersonID.Value)),用于打印0。 你得到你想要的结果:

enter image description here

性能会更轻松,更好。