【发布时间】:2017-05-19 10:56:36
【问题描述】:
我有一种情况,我想查询多个属性(总共约 8 个)并包括小计。这是我想要的结果:
╔═══════╦═════════╦════════╦═════════╗
║ Attr1 ║ Attr2 ║ Attr3 ║ Measure ║
╠═══════╬═════════╬════════╬═════════╣
║ All ║ All ║ All ║ 50% ║
║ Foo ║ All ║ All ║ 25% ║
║ Bar ║ All ║ All ║ 90% ║
║ Foo ║ Anna ║ All ║ 42% ║
║ Foo ║ Brian ║ All ║ 12% ║
║ Bar ║ Charles ║ All ║ 10% ║
║ Bar ║ Dory ║ All ║ 112% ║
║ Foo ║ Anna ║ Box ║ 58% ║
║ Foo ║ Anna ║ Circle ║ 13% ║
║ ... ║ ... ║ ... ║ ... ║
╚═══════╩═════════╩════════╩═════════╝
现在,我几乎可以通过这样做:
select
{[Measures].[Measure]} on columns,
nonempty({
[Dim1].[Attr1].allmembers *
[Dim2].[Attr2].allmembers *
[Dim3].[Attr3].allmembers
}) on rows
from [Cube]
不过,这当然让我得到了一个包含如下成员的集合:
╔═══════╦═════════╦════════╦═════════╗
║ Attr1 ║ Attr2 ║ Attr3 ║ Measure ║
╠═══════╬═════════╬════════╬═════════╣
║ Foo ║ All ║ Box ║ 25% ║
║ Bar ║ All ║ Circle ║ 90% ║
║ Foo ║ Anna ║ Box ║ 16% ║
║ Bar ║ Charles ║ Circle ║ 78% ║
║ ... ║ ... ║ ... ║ ... ║
╚═══════╩═════════╩════════╩═════════╝
我不想要 - 我可以和他们一起生活,除了 8 维它让交叉连接有点疯狂(它给我一个错误,关于有一个包含超过 40 亿个元组的集合它...)。现在,如果我正在编写 SQL,我可以做一些简单的事情,比如:
select
Dim1.Attr1,
Dim2.Attr2,
Dim3.Attr3,
Sum(Measures.Measure) as Measure
group by
Dim1.Attr1,
Dim2.Attr2,
Dim3.Attr3
with rollup
但我找不到在 MDX 中重现此问题的简单方法。我可以像这样手动构建每个汇总级别:
select
{[Measures].[Measure]} on columns,
nonempty(
{
{[Dim1].[Attr1].[All]} *
{[Dim2].[Attr2].[All]} *
{[Dim3].[Attr3].[All]}
} +
{
{[Dim1].[Attr1].[Attr1].allmembers} *
{[Dim2].[Attr2].[All]} *
{[Dim3].[Attr3].[All]}
} +
{
{[Dim1].[Attr1].[Attr1].allmembers} *
{[Dim2].[Attr2].[Attr2].allmembers} *
{[Dim3].[Attr3].[All]}
} +
{
{[Dim1].[Attr1].[Attr1].allmembers} *
{[Dim2].[Attr2].[Attr2].allmembers} *
{[Dim3].[Attr3].[Attr3].allmembers}
}
) on rows
from [Cube]
但是仅使用三个维度已经变得乏味 - 指定其中的 9 个组将是令人讨厌的。那么 - 有没有办法在 MDX 中简洁地做到这一点,还是我只需要采用长期解决方案?
就先前的研究而言,我遇到过很多答案,例如 this one,它们说要使用 WITH MEMBER 语句来创建一个总行 - 但这对我来说毫无意义,因为它会导致相同的交叉- 加入我试图通过allmembers 函数避免的行为。
编辑:这是代码的最新(净化)版本,包括@Danylo 对NonEmptyCrossJoin 的建议:
NON EMPTY {
NONEMPTYCROSSJOIN(
{[Dim1].[Attribute].[All]} *
{[Dim2].[Attribute].[All]} *
{[Dim3].[Attribute].[All]} *
{[Dim4].[Attribute].[All]} *
{[Dim6].[Attribute].[All]} *
{[Dim7].[Attribute].[All]} *
{[Dim8].[Attribute].[All]} *
{[Dim9].[Attribute].[All]} *
[Dim0].[Attribute].[Attribute].ALLMEMBERS
) +
NONEMPTYCROSSJOIN(
[Dim1].[Attribute].[Attribute].ALLMEMBERS *
{[Dim2].[Attribute].[All]} *
{[Dim3].[Attribute].[All]} *
{[Dim4].[Attribute].[All]} *
{[Dim6].[Attribute].[All]} *
{[Dim7].[Attribute].[All]} *
{[Dim8].[Attribute].[All]} *
{[Dim9].[Attribute].[All]} *
[Dim0].[Attribute].[Attribute].ALLMEMBERS
) +
NONEMPTYCROSSJOIN(
[Dim1].[Attribute].[Attribute].ALLMEMBERS *
[Dim2].[Attribute].[Attribute].ALLMEMBERS *
{[Dim3].[Attribute].[All]} *
{[Dim4].[Attribute].[All]} *
{[Dim6].[Attribute].[All]} *
{[Dim7].[Attribute].[All]} *
{[Dim8].[Attribute].[All]} *
{[Dim9].[Attribute].[All]} *
[Dim0].[Attribute].[Attribute].ALLMEMBERS
) +
...
}
【问题讨论】:
-
您是否尝试过使用度量作为 NonEmpty() 函数的第二个参数来减小交叉连接集的大小?这不会帮助您解决“像 WITH ROLLUP 这样的选择性总计”问题,但至少可以使交叉连接成为可能。
标签: sql-server ssas mdx rollup