数据多维数据集查找列的最大值

时间:2019-12-12 05:31:49

标签: sql cube

我创建了一个具有以下特征的名为Tb_Offers_Cube的多维数据集。问题在于使用max函数查找每个城市提供最多数量的产品。我仍在为每个城市购买多种产品。

The dimensions of the cube are: Tb_Supplier and Tb_Product.
Measure groups table is: Tb_Offers.
Measure aggregates: SUM(Quantity), SUM(Quantity*Price), 
MAX(Price) , MIN(Price).
Dimension hierarchies:

Tb_Supplier:    State > City > Name
Tb_Product:     Product_Packaging  > Name 
                Product_Category > Product_Line > Name

--For each supplier city find the product offered in largest quantity?
    SELECT [Supplier City], [Product Name], max(DISTINCT [Total Transactions Quantity])
    FROM Tb_Offers_Cube
    WHERE [Supplier Name] is NULL
    AND [Supplier State] is NOT NULL
    AND [Supplier City] is NOT NULL
    AND [Product Name] is NOT NULL
    AND [Product Packaging] is NOT NULL
    AND [Product Category] is NULL
    AND [Product Line] is NULL
    GROUP BY [Supplier City], [Product Name], [Total Transactions Quantity]
    ORDER BY [Supplier City], [Total Transactions Quantity] DESC;

1 个答案:

答案 0 :(得分:0)

对于每个max,您可以获取city 产品名称,以包含在您的显示中。

SELECT [Supplier City]    
    , [Total Transactions Quantity]
    , (SELECT TOP 1 [Product Name] FROM Tb_Offers_Cube WHERE [Total Transactions Quantity] = t.[Total Transactions Quantity]
        AND [Supplier City] = t1.[Supplier City]) AS [Product Name]
FROM 
    (SELECT [Supplier City]    
        , max(DISTINCT [Total Transactions Quantity]) [Total Transactions Quantity]
    FROM Tb_Offers_Cube
    WHERE [Supplier Name] is NULL
    AND [Supplier State] is NOT NULL
    AND [Supplier City] is NOT NULL
    AND [Product Name] is NOT NULL
    AND [Product Packaging] is NOT NULL
    AND [Product Category] is NULL
    AND [Product Line] is NULL
    GROUP BY [Supplier City]) t
ORDER BY t.[Supplier City], t.[Total Transactions Quantity] DESC;
相关问题