Skip to content
Advertisement

What causes error “argument of HAVING must be type boolean, not type money”, and how do I fix it?

I am trying to make a query to see the total price a customer has to pay when their order is completed, but I am having issues with this error statement. How do I get the correct query?

ERROR: argument of HAVING must be type boolean, not type money

Tables involved and inserting some fake quick data:

Some of the customers:

Some of the products:

Then I combined these tables into a many-to-many relation table ordered_items:

As the customer buys an item in the store at the same time, I subtracted the original quantity they had in the store. Say, if they had 100 apples and the customer bought 5, the store has 95 apples left. This bit is all so far so good. Example:

However, now when I try query the final price the customer has to pay, I am getting to this error

ERROR: argument of HAVING must be type boolean, not type money

And here is my query code so far:

How can I fix that?

I want the results to give this:

And not this, which I am getting if I don’t include any HAVING or WHERE CLAUSE where I am trying to limit and have the price * quantity

How should I go about and get this query; I want price * quantity to show the right:

Advertisement

Answer

Cast 0 to money to give zero the same type as the calculation and thus provide for a legal comparison:

See live demo.

Note also that you must use the aggregated (ie sum) value of ordered_items.quantity

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement