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 8271689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:54:42+00:00 2026-06-08T06:54:42+00:00

My first time working with CASE Logic in SQL statements. Everything works if I

  • 0

My first time working with CASE Logic in SQL statements. Everything works if I remove the CASE statements, so the SQL is valid without it.

I need to calculate the total item price based on a couple of things.

If “Sales Price” is active AND “Option Upcharge” has a value, the total is: Qty * (Sales Price + Option Upcharge)

If “Sales Price is inactive AND “Option Upcharge” has a value, the total is: Qty * (Price + Option Upcharge)

If “Sales Price” is active AND “Option Upcharge” has NO value, the total is: Qty * Sales Price

If “Sales Price is inactive AND “Option Upcharge” has NO value, the total is: Qty * Price

If no Option was added, the value for tblproduct_options.option_upcharge is NULL in the output.

Thanks for the help.

Brett

Here is my SQL:

SELECT tblshopping_cart.session_id, tblshopping_cart.product_id, tblshopping_cart.product_qty, tblshopping_cart.product_option, tblproducts.product_title, tblproducts.product_price, tblproducts.product_sale_price_status, tblproducts.product_sale_price, tblproduct_options.option_text, tblproduct_options.option_upcharge,
CASE
WHEN (tblproducts.product_sale_price_status = 'Y')
    CASE
    WHEN (tblproduct_options.option_upcharge IS NOT NULL)
        THEN (tblshopping_cart.product_qty * (tblproducts.product_sale_price + tblproduct_options.option_upcharge)) 
        ELSE (tblshopping_cart.product_qty * tblproducts.product_sale_price)    
    END
ELSE
    CASE
    WHEN (tblproduct_options.option_upchage IS NOT NULL)
        THEN (tblshopping_cart.product_qty * (tblproducts.product_price + tblproduct_options.option_upcharge))
        ELSE (tblshopping_cart.product_qty * tblproducts.product_price)
    END
END AS product_total
FROM tblshopping_cart
INNER JOIN tblproducts ON tblshopping_cart.product_id = tblproducts.product_id
LEFT JOIN tblproduct_options ON tblshopping_cart.product_option = tblproduct_options.option_product_id
ORDER BY tblshopping_cart.product_qty ASC

It fails with with message:

CASE
    WHEN (tblproduct_options.option_upcharge IS NOT NULL)
        THEN (tblshopping_' at line 4
  • 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-06-08T06:54:44+00:00Added an answer on June 8, 2026 at 6:54 am

    You are missing a THEN in your first CASE Statement. (sorry I had to add table aliases)

    SELECT sc.session_id
        , sc.product_id
        , sc.product_qty
        , sc.product_option
        , p.product_title
        , p.product_price
        , p.product_sale_price_status
        , p.product_sale_price
        , po.option_text
        , po.option_upcharge
        , CASE
            WHEN (p.product_sale_price_status = 'Y')
            THEN <-- add this
                CASE
                WHEN (po.option_upcharge IS NOT NULL)
                    THEN (sc.product_qty * (p.product_sale_price + po.option_upcharge)) 
                    ELSE (sc.product_qty * p.product_sale_price)    
                END
            ELSE
                CASE
                WHEN (po.option_upchage IS NOT NULL)
                    THEN (sc.product_qty * (p.product_price + po.option_upcharge))
                    ELSE (sc.product_qty * p.product_price)
                END
            END AS product_total
    FROM tblshopping_cart sc
    INNER JOIN tblproducts p
        ON sc.product_id = p.product_id
    LEFT JOIN tblproduct_options po
        ON sc.product_option = po.option_product_id
    ORDER BY sc.product_qty ASC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First time working with JSON in Rails and trying to figure out something which
This is my first time working with a WPF datagrid. From what I understand
This is my first time working with file i/o in java, and it's not
I am having an problem. First time working with a windows server, do you
We're building a very content rich site in Drupal. It's my first time working
Currently working for the first time with JSON and with little experience of jQuery.
I'm working in Python for the first time and I've used Mechanize to search
i am working inapp-purchase project and first time for creating inapp-purchase in new app
I'm working with oracle for the first time, and I have a table called
im working with core-data for the first time, and my app is handling a

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.