I’m working on my first SQL case statement – and unsure how to do this. I have two different columns of data, one is for drive A, the other drive B. I want to select data from drive A for display when drive A is active, and display data from drive B when it is active. I’m having trouble understanding how to use the case statement as a part of the select statement.
I know that the case statement returns a boolean value, but I’m unsure of how to incorporate this into my Select expression. Right now I have:
SELECT t_stamp AS DateTime,
CASE WHEN DriveAActive
THEN DriveA_TorqueActual
ELSE DriveB_TorqueActual
I don’t see a good reference on this topic – any pointers?
2
Answers
Mysql CASE statement is simple and syntax like:
Your question is not clear. According to question there are two columns
DriveA
&DriveB
. Need to display data which one is ‘Active’.I think
DriveA
has high priority. So, design code as follows:OR
I believe you are missing a closing END keyword to the CASE statement in your sql query. Also, as mentioned by the other user that’s answered above, The question is quite ambigous as one does not know which of the columns have priority over the other should both columns i.e columns DriveA and DriveB both be active at the same time. In any case, if only one column can be active at a time then the code below would suffice. Else, you would have to adjust the conditions within each WHEN statement to cater for that scenario.