为每个消费者找到购买的最便宜的产品

时间:2018-12-08 18:03:51

标签: sql cube

我正在尝试获取每个消费者从多维数据集购买的最便宜产品的列表。我尝试了以下多维数据集查询:

SELECT "Consumer Name","Product Name",MIN([Total Price]) AS "Total Purchase Price"
FROM Transaction_Cube 
WHERE "Consumer Name" IS NOT NULL
    AND "Consumer City" IS NOT NULL
    AND "Consumer State" IS NOT NULL       
    AND "Product Name" IS NOT NULL 
    AND "Product Category" IS NOT NULL
    AND "Product Line" IS NOT NULL
    AND "Product Packaging" IS NOT NULL
GROUP BY "Consumer Name","Product Name"

但这给了我以下结果:

enter image description here

我从中获得每种消费者/产品组合的最低购买价。但是我想要达到的目标是这样的:

enter image description here

我只为每个消费者提供最便宜的产品。关于如何纠正此查询的任何想法或建议,将不胜感激!

1 个答案:

答案 0 :(得分:1)

您不需要GROUP BY "Consumer Name","Product Name"
检查一下:

SELECT t.[Consumer Name], t.[Product Name], t.[Total Price] AS "Total Purchase Price"
FROM Tb_Transactions_Cube_FE t
WHERE 
t.[Total Price] = (SELECT MIN([Total Price]) FROM Tb_Transactions_Cube_FE WHERE [Consumer Name] = t.[Consumer Name])
ORDER BY t.[Consumer Name]