3

I am trying to solve following problem:

I have a table with timeStamp column and three columns for different types of variables. The columns with values are filled randomly according to the conditions. So it looks like this:


smpl_time  float_val  num_val  str_val
15:31         10            NULL      NULL
15:32         NULL       15.4        NULL
15:33         NULL        NULL      Disabled

What I would like to achieve is a result that would merge all val fields into one column and sorts them according to timeStamp:


smpl_time merge_val
15:31        10
15:32        15.4
15:33        Disabled



So far I have a SELECT just for one val type looking like this (it contains also other field from the table that are common for each entry):

SELECT s.smpl_time AS Time,s.float_val AS Value, e.name AS Severity, m.name AS Status 
FROM sample s JOIN channel c ON c.channel_id=s.channel_id JOIN severity e ON
e.severity_id=s.severity_id JOIN status m ON m.status_id=s.status_id WHERE
s.channel_id='id' AND smpl_time BETWEEN TIMESTAMP 'startTime' AND TIMESTAMP 'stopTime';


Does anyone have an idea how to do this or if it is even possible?

Cheers,

Mike

2 Answers 2

2

Assuming only one of the three columns is populated for any given row, use the COALESCE function.

SELECT smpl_time, COALESCE(float_vaL, num_val, str_val) AS merge_val
    FROM ...
Sign up to request clarification or add additional context in comments.

1 Comment

IIRC If the 1st row resulted in an int and the 2nd in a char type you would need a cast to a type that supported both (eg char)
2

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce

COALESCE(value,...)

Returns the first non-NULL value in the list, or NULL if there are no non-NULL values.

mysql> SELECT COALESCE(NULL,1); -> 1

mysql> SELECT COALESCE(NULL,NULL,NULL); -> NULL

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.