Jump to content

Wrong sum output from case statement condition


Recommended Posts


I have data:
shift_id               = 1---2---3---4---5
compound_output = 5---10--20--50--10
process_id = 7
compound_type = P28

and now I need to create a query that sum the value of (1+3+(5/ 2)) and (2+4+(5/ 2))

here is my query:

SELECT process_id, compound_type, CASE WHEN (shift_id IS NOT NULL AND (shift_id = 1 OR shift_id = 3 OR shift_id = 5))
THEN SUM((compound_output)/2)
when (shift_id IS NOT NULL AND (shift_id = 2 OR shift_id = 4 OR shift_id = 5))
THEN SUM((compound_output)/2)  END AS val
FROM op_output WHERE process_id = 7 and compound_type = 'P28' HAVING val IS NOT NULL

and the output of this is:

val = 47.500000

but it should be:

Thank you so much..

I hope somebody can help me to get the correct values.

Thank you so much.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.