SQL 查询多维数据集并汇总

SQL query cubes and roll up

目前正在尝试找到最大值,对于芝加哥,应该只显示计算机,因为它的数量是 1010,它是最大的数量

下面是对上述多维数据集执行的查询。我只是想找到每个城市产品名称的最大数量。

SELECT [Supplier City], [Product Name], [Total Transactions Quantity]
FROM Tb_Final_Cube
WHERE "Supplier Name" IS NULL
AND "Supplier City" IS NOT NULL
AND "Supplier State" IS NOT NULL
AND "Product Packaging" IS NOT NULL
AND "Product Name" IS NOT NULL
AND "Product Category" IS NULL
AND "Product Line" IS NULL

使用window函数:

SELECT tfb.*
FROM (SELECT [Supplier City], [Product Name], [Total Transactions Quantity],
             ROW_NUMBER() OVER (PARTITION BY [Product Name] ORDER BY [Total Transactions Quantity] DESC) as seqnum
      FROM Tb_Final_Cube
      WHERE "Supplier Name" IS NULL AND
            "Supplier City" IS NOT NULL AND
            "Supplier State" IS NOT NULL AND
            "Product Packaging" IS NOT NULL AND
            "Product Name" IS NOT NULL AND
            "Product Category" IS NULL AND
            "Product Line" IS NULL 
      ) tfb
WHERE seqnum = 1;