0

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?

6
  • From 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 it seems to me that you need WHERE DriveA='active' AND DriveB='active'; not a case expression Commented Nov 27, 2024 at 16:07
  • What do you want to display if both A and B are active? Commented Nov 27, 2024 at 16:57
  • You just need to add END and an alias for the CASE expression. Commented Nov 27, 2024 at 17:20
  • What is the datatype of DriveAActive? Your espression will work with a BOOLEAN or INT (where 0 is FALSE, anything else is TRUE). Commented Nov 27, 2024 at 17:21
  • If it's something else, you need something like CASE WHEN DriveAActive = 'yes' Commented Nov 27, 2024 at 17:23

2 Answers 2

0

Mysql CASE statement is simple and syntax like:

CASE
   WHEN condition_1 THEN result_1
   WHEN condition_2 THEN result_2 
   ... ... ...
   WHEN condition_m THEN result_m
   ELSE result_n
END AS column_name

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:

  SELECT
      t_stamp AS DateTime, 
      CASE 
          WHEN(DriveA = 'Active')
            THEN DriveA_TorqueActual 
          WHEN(DriveB = 'Active')
            THEN DriveB_TorqueActual
          ELSE NULL
      END AS ACTIVE_DRIVE
  FROM SQL_TABLE

OR

  SELECT
      t_stamp AS DateTime, 
      CASE 
          WHEN(DriveA = 'Active' AND DriveB = 'Active')
            THEN result1
          WHEN(DriveA = 'Active' AND DriveB <> 'Active')
            THEN result2
          WHEN(DriveA <> 'Active' AND DriveB = 'Active')
            THEN result3
          ELSE result4
      END AS ACTIVE_DRIVE
  FROM SQL_TABLE
Sign up to request clarification or add additional context in comments.

Comments

0

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.

SELECT t_stamp AS DateTime, 
    CASE WHEN DriveAActive 
         THEN DriveA_TorqueActual 
         ELSE DriveB_TorqueActual
    END AS alias
FROM table_name

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.