【问题标题】:MDX Cube SQL Queries - Just want a row showing the Minimum price of a product in a city to show up, NOT all pricesMDX Cube SQL 查询 - 只需要显示某城市产品最低价格的行,而不是所有价格
【发布时间】:2020-04-07 07:33:42
【问题描述】:

我不知道如何防止每个产品显示多行。

这是我的代码:

-- For each product find the city where it is offered at the lowest price
WITH MEMBER [Measures].[MIN Price] AS MIN([Measures].[Price]) 

SELECT NON EMPTY { 
[Measures].[MIN Price] } ON COLUMNS, 
NON EMPTY { 
([Tb Product].[Name].[Name].ALLMEMBERS * [Tb Supplier].[City].[City].ALLMEMBERS ) } ON ROWS 
FROM [DS715]

这是结果的屏幕截图。

我不想让每个城市都出现,只显示价格最低的城市

我希望查询返回:

Boat     Wausau       3013.17
Computer Springfield  450.89
Gas      Wausau       2.28
Milk     Madison      3.88
Orange   Wausau       1.89
TV       Wausau       189.99

编辑:尝试使用以下代码的顺序和排名,但现在每个产品名称都显示一个巨大的数字

代码:

WITH MEMBER [Measures].[Max Price RANK] AS 
RANK( ([Tb Product].[Name].currentmember), 
ORDER( ([Tb Product].[Name].currentmember), [Measures].[Price - Tb Transactions], BDESC) ) 

SELECT 
NON EMPTY { 
    [Measures].[Price - Tb Transactions] } ON COLUMNS, 
NON EMPTY { 
    filter([Tb Product].[Name].[Name], [Measures].[Max Price RANK] <2 )} ON ROWS 
FROM [DS715] 
WHERE ( [Tb Supplier].[City].&[Madison] )

【问题讨论】:

  • 看来您需要找到一种方法将 distinct 关键字放入查询中。
  • @robbpriestley 这不是 SQL。 MDX 的工作方式不同
  • 我想知道它是否应该被标记为 sql-server 呢?

标签: sql sql-server ssas mdx min


【解决方案1】:

尝试 generate 语句:

https://docs.microsoft.com/en-us/sql/mdx/generate-mdx?view=sql-server-ver15

SELECT 
{[Measures].[Price]}
ON COLUMNS, 
{
    Generate([Tb Product].[Name].[Name].ALLMEMBERS,                             // foreach product
         [Tb Product].[Name].CURRENTMEMBER *                                    // cross the product
         BOTTOMCOUNT([Tb Supplier].[City].[City].ALLMEMBERS, 1,                    // with bottom 1 city
                       ([Tb Product].[Name].CURRENTMEMBER, [Measures].[Price])) // by product and price
    )
}

ON ROWS

FROM [DS715]

【讨论】:

    【解决方案2】:

    您的代码不起作用,因为您错误地使用了 Rank。进行以下修正

    WITH MEMBER [Measures].[Max Price RANK] AS 
    RANK( ([Tb Product].[Name].currentmember,[Tb Supplier].[City].currentmember), 
    ORDER( ([Tb Product].[Name].currentmember,[Tb Supplier].[City].[City].members), [Measures].[Price - Tb Transactions], BDESC) 
    ) 
    
    SELECT 
    NON EMPTY { 
        [Measures].[Price - Tb Transactions] } ON COLUMNS, 
    NON EMPTY { 
        ([Tb Product].[Name].[Name],filter([Tb Supplier].[City].[City], [Measures].[Max Price RANK] <2 )} 
        ON ROWS 
    FROM [DS715] 
    

    【讨论】:

    • @obizues 以上有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 2021-12-17
    • 2013-02-25
    相关资源
    最近更新 更多