select storeID, itemID, custID, sum(price)
from Sales F
group by storeID, custID, itemID 
    with cube(storeID, custID);我打算在postgreSQL中使用此查询,但它不能直接在postgreSQL中工作,如何将此查询转换为postgreSQL查询?
发布于 2020-12-11 20:00:06
您的代码将在没有with关键字的情况下工作:
select storeID, itemID, custID, sum(price)
from Sales F
group by itemID, cube(storeID, custID);我更喜欢使用分组集来表示分组:
select storeID, itemID, custID, sum(price)
from Sales F
group by grouping sets ( (storeID, custID, itemID),
                         (custID, itemID),
                         (storeID, itemID),
                         (itemID)
                       );如果我理解你想要做什么,这应该是完全相同的。
也可以使用cube,然后使用filter:
select storeID, itemID, custID, sum(price)
from Sales F
group by cube(storeID, custID, itemID)
having itemId is not null发布于 2020-12-11 18:44:44
试试这个:
select storeID, itemID, custID, sum(price)
from Sales F
group by cube (storeID, itemID, custID)有关CUBE、GROUPING SETS和ROLLUP.的更多详细信息,请查看this post 
https://stackoverflow.com/questions/65249764
复制相似问题