Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6744269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:03:49+00:00 2026-05-26T12:03:49+00:00

I have a products database with multiple tables to store pricing (because of default

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:03:49+00:00Added an answer on May 26, 2026 at 12:03 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database with these tables: products,colors,sizes Each product(t-shirts,jeans etc) can have multiple
I have a number of tables to store different product types in my database
I have a database table which stores products. Each product can have multiple colours.
I have a table products and table sizes. Each product can have multiple sizes.
I have a PRODUCTS table, and each product can have multiple attributes so I
I have an e-commerce MySQL database with the following tables: **Product** ---------- productID -
I have the following database structure (simplified) Store StoreId RateId Products ProductId Name Rates
I have products database like: id: 5 catid: 2,4,26,33,46 name: product one etc... For
I have multiple tables in a mysql database and I am trying to put
I have a MySQL database with a fairly large table where the products are.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.