Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql CASE statement - can I use the return value of the CASE within my SELECT?

Tags:

postgresql

I have a products database with multiple tables to store pricing (because of default pricing and optional override pricing per product colour), and I have CASE statements in my query to get the * Original price of the product * Sale price of the product, if it is on sale

I also need to calculate the DISCOUNT of the product if it is on sale. Before I try this, please see the breakdown of my existing SQL which works.

SELECT  p.productid, p.stylename,
CASE    WHEN (ppo.original_price IS NOT NULL) THEN ppo.original_price ELSE pp.original_price END AS final_original_price,
CASE    WHEN (ppo.original_price IS NOT NULL) THEN ppo.sale_price ELSE pp.sale_price END AS final_sale_price

FROM product p, ... etc.

The above code works (I have simplified it), and basically, the original price of the product is stored in the column alias "final_original_price", and the sale price (which might be NULL) is returned as "final_sale_price".

I now want to add an additional line to the SELECT to get the discount. I can't use the existing fields in the actual table because I want those return values of "final_original_price" and "final_sale_price" to do the calculations.

e.g.

SELECT  p.productid, p.stylename,
CASE    WHEN (ppo.original_price IS NOT NULL) THEN ppo.original_price ELSE pp.original_price END AS final_original_price,
CASE    WHEN (ppo.original_price IS NOT NULL) THEN ppo.sale_price ELSE pp.sale_price END AS final_sale_price,

((final_original_price - final_sale_price) / final_original_price * 100) AS final_discount_percentage 

FROM product p, ... etc.

The above does not work, as Postgresql returns "ERROR: column "final_original_price" does not exist at character....."

So, clearly I can't use the return value of the CASE. My questions on what solution to use:

1) Can I indeed use the return value of the case like I want to, above? OR 2) Do I need to again plug in the case statement to my calculation? That means I need to repeat the CASE code and the query will look quite lengthy. If I have to, I'd do this, but I'm just wondering if there's a better way. OR 3) I could also store an additional field in the database to store the discount. This data would be redundant because my CMS would need to ensure that the field is updated whenever the price is updated. However it would save heavy calculations (if the above is considered heavy) on the front end which runs much more often than the backend CMS.

The above solutions are probably the easiest, but I am also wondering, if I had time to do this, are there any other solutions here that would be worth considering? For example would this be a good scenarios to write a "view"? Personally I have never set up a view and as far as I understand, the database-selection work is still happening in the background, but, if set up, would make the end query above simpler to understand from a developer's point of view.

Many thanks!

like image 397
rishijd Avatar asked Oct 31 '11 07:10

rishijd


People also ask

Can I use case in WHERE clause PostgreSQL?

The PostgreSQL CASE expression is the same as IF/ELSE statement in other programming languages. It allows you to add if-else logic to the query to form a powerful query. Since CASE is an expression, you can use it in any places where an expression can be used e.g., SELECT , WHERE , GROUP BY , and HAVING clause.

How does CASE statement work in PostgreSQL?

Introduction to PostgreSQL CASEEach condition is a boolean expression and based on its output the result is chosen. If all the expressions corresponding to WHEN are evaluated to be False , then the result respective to the ELSE part is shown. In case, you don't specify the ELSE part; the query will return null.

How do I return a selected query in PostgreSQL?

To return a table from the function, you use RETURNS TABLE syntax and specify the columns of the table. Each column is separated by a comma (, ). In the function, we return a query that is a result of a SELECT statement.

Is PostgreSQL values case sensitive?

PostgreSQL is a case-sensitive database by default, but provides various possibilities for performing case-insensitive operations and working with collations.


1 Answers

use

SELECT
productid, 
stylename,
final_original_price,
final_sale_price,
((final_original_price - final_sale_price) / final_original_price * 100) AS final_discount_percentage 
FROM
(
SELECT  p.productid, p.stylename,
CASE    WHEN (ppo.original_price IS NOT NULL) THEN ppo.original_price ELSE pp.original_price END AS final_original_price,
CASE    WHEN (ppo.original_price IS NOT NULL) THEN ppo.sale_price ELSE pp.sale_price END AS final_sale_price
FROM product p, ... etc.
)

The above does exactly what you asked for... if for some reason you don't want to use it then plugin the CASE statements into the calculation (option 2 from your question).

like image 85
Yahia Avatar answered Sep 27 '22 16:09

Yahia