0
select storeID, itemID, custID, sum(price)
from Sales F
group by storeID, custID, itemID 
    with cube(storeID, custID);

I am going to use this query in postgreSQL but it doesn't work in postgreSQL directly how can I convert this query into postgreSQL query?

2
  • If think PostgreSQL's terminology uses ROLLUP instead of CUBE. Commented Dec 11, 2020 at 10:43
  • Your MySQL query is not valid: dbfiddle.uk/… What is the expected result Commented Dec 11, 2020 at 10:52

2 Answers 2

1

Your code will work without the with keyword:

select storeID, itemID, custID, sum(price)
from Sales F
group by itemID, cube(storeID, custID);

I prefer grouping sets for expressing groupings:

select storeID, itemID, custID, sum(price)
from Sales F
group by grouping sets ( (storeID, custID, itemID),
                         (custID, itemID),
                         (storeID, itemID),
                         (itemID)
                       );

If I understand what you want to do, this should be exactly the same.

You could also use cube and then filter:

select storeID, itemID, custID, sum(price)
from Sales F
group by cube(storeID, custID, itemID)
having itemId is not null
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

select storeID, itemID, custID, sum(price)
from Sales F
group by cube (storeID, itemID, custID)
  • Check this post for more details on CUBE,GROUPING SETS and ROLLUP.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.