3

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

以“Person”的构造为例,其列: - name - gender - 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 个数据集来驱动矩阵......

  • “行数据”包含select distinct marital_status from person
  • "ColumnData" 包含select distinct gender from person, 和
  • “MainData”包含`select * from person where [...criteria]

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

4

1 回答 1

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 中: 在此处输入图像描述

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

在此处输入图像描述

这将更容易和更好的性能。

于 2013-02-12T07:36:23.100 回答