使用“父”的过滤条件现任会员

发布于 2025-01-09 11:08:18 字数 1730 浏览 4 评论 0原文

这是数据集:

CREATE TABLE Movies(id INT, name VARCHAR(50), genre VARCHAR(50), budget DECIMAL(10));
INSERT INTO Movies VALUES
    (1, 'Pirates of the Caribbean', 'Fantasy', 379000000),
    (2, 'Avengers', 'Superhero', 365000000),
    (3, 'Star Wars', 'Science fiction', 275000000),
    (4, 'John Carter', 'Science fiction', 264000000),
    (5, 'Spider-Man', 'Superhero', 258000000),
    (6, 'Harry Potter', 'Fantasy', 250000000),
    (7, 'Avatar', 'Science fiction', 237000000);

相对于恒定值进行过滤没有问题,例如获取预算高于 3 亿美元的所有电影:

WITH
    MEMBER X AS SetToStr(Filter(Movie.[Name].[Name].Members - Movie.[Name].CurrentMember, Measures.Budget > 300000000))
SELECT
    Movie.[Name].[Name].Members ON ROWS,
    X ON COLUMNS
FROM
    Cinema

给出:

Avatar          {[Movie].[Name].&[Avengers],[Movie].[Name].&[Pirates of the Caribbean]}
Avengers        {[Movie].[Name].&[Pirates of the Caribbean]}
Harry Potter    {[Movie].[Name].&[Avengers],[Movie].[Name].&[Pirates of the Caribbean]}
John Carter     {[Movie].[Name].&[Avengers],[Movie].[Name].&[Pirates of the Caribbean]}
Pirates of the Caribbean    {[Movie].[Name].&[Avengers]}
Spider-Man      {[Movie].[Name].&[Avengers],[Movie].[Name].&[Pirates of the Caribbean]}
Star Wars       {[Movie].[Name].&[Avengers],[Movie].[Name].&[Pirates of the Caribbean]}

但是如何与当前电影的预算而不是硬预算进行比较编码 3 亿美元以获得比当前电影更贵的电影?

对于“加勒比海盗”,它会给出 {},因为它是最昂贵的电影。
对于“复仇者联盟”来说,它是{ '加勒比海盗' },因为这是第二贵的,只有“加勒比海盗”更贵。
对于“阿凡达”,它会给予所有其他电影,因为它更便宜。

问题在于,Filter 函数的条件 CurrentMember 引用了当前测试的元组,而不是当前在 ROWS 轴上选择的元组。

Here is the data-set:

CREATE TABLE Movies(id INT, name VARCHAR(50), genre VARCHAR(50), budget DECIMAL(10));
INSERT INTO Movies VALUES
    (1, 'Pirates of the Caribbean', 'Fantasy', 379000000),
    (2, 'Avengers', 'Superhero', 365000000),
    (3, 'Star Wars', 'Science fiction', 275000000),
    (4, 'John Carter', 'Science fiction', 264000000),
    (5, 'Spider-Man', 'Superhero', 258000000),
    (6, 'Harry Potter', 'Fantasy', 250000000),
    (7, 'Avatar', 'Science fiction', 237000000);

To filter relatively to a constant value no problem, e.g. to get all the movies with a budget higher than 300M$:

WITH
    MEMBER X AS SetToStr(Filter(Movie.[Name].[Name].Members - Movie.[Name].CurrentMember, Measures.Budget > 300000000))
SELECT
    Movie.[Name].[Name].Members ON ROWS,
    X ON COLUMNS
FROM
    Cinema

Which gives:

Avatar          {[Movie].[Name].&[Avengers],[Movie].[Name].&[Pirates of the Caribbean]}
Avengers        {[Movie].[Name].&[Pirates of the Caribbean]}
Harry Potter    {[Movie].[Name].&[Avengers],[Movie].[Name].&[Pirates of the Caribbean]}
John Carter     {[Movie].[Name].&[Avengers],[Movie].[Name].&[Pirates of the Caribbean]}
Pirates of the Caribbean    {[Movie].[Name].&[Avengers]}
Spider-Man      {[Movie].[Name].&[Avengers],[Movie].[Name].&[Pirates of the Caribbean]}
Star Wars       {[Movie].[Name].&[Avengers],[Movie].[Name].&[Pirates of the Caribbean]}

But how to compare to the budget of the current movie instead of the hard-coded 300M$ to get the movies more expensive than the current one?

It would give {} for "Pirates of the Caribbean" as it is the most expensive movie.
For "Avengers" it would be { 'Pirates of the Caribbean' } as this is the second most expensive and only "Pirates of the Caribbean" is more expensive.
For "Avatar" it would give all the other movies as it is the less expensive.

The issue is that inside the Filter function's condition CurrentMember refers to the currently tested tuple and not the one currently selected on the ROWS axis.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

享受孤独 2025-01-16 11:08:18

我不会对每部电影使用 Filter(),而是首先根据预算值计算一组有序的电影。然后可以使用 SubSet排名功能。

这是一个使用不同模式的示例,但我想您会很容易明白这一点:

with 
   set ordered_continents as order( [Geography].[Geography].[Continent], -[Measures].[#Sales] )
   member xx as SetToStr( SubSet( ordered_continents, 0, Rank( [Geography].[Geography].currentMember, ordered_continents) - 1))
   
select {[#Sales], [xx] } on 0, [Geography].[Geography].[Continent] on 1 from [Sales] 

在此处输入图像描述

我不熟悉 SSAS,所以我使用 icCube,但我想 MDX 应该非常相似。

Instead of using Filter() for each movie, I would first compute an ordered set of movies based on budget values. Then X could be defined using the SubSet and Rank function.

Here is an example using a different schema but I guess you'll get the point easily:

with 
   set ordered_continents as order( [Geography].[Geography].[Continent], -[Measures].[#Sales] )
   member xx as SetToStr( SubSet( ordered_continents, 0, Rank( [Geography].[Geography].currentMember, ordered_continents) - 1))
   
select {[#Sales], [xx] } on 0, [Geography].[Geography].[Continent] on 1 from [Sales] 

enter image description here

I'm not familiar with SSAS so I'm using icCube but I guess the MDX should be very much similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文