1

I'm really confused about this query. I would like to get all the data for one context in one row. I really don't know how to do that. Here's the tables's sample.

data (table)                               io(table)
IDData    IDIO   ReadVal   No              IDIO   IDContext
1          io1     12       1              io1       c1     
2          io2     12.5     2              io2       c1 
3          io3     11       3              io3       c1 
4          io4     12.2     1              io4       c2  
5          io5     10       2              io5       c2  
6          io6     10.9     3              io6       c2

I would like to get this result

IDContext    IO-1   IO-2   IO-3
   c1         12    12.5    11
   c2        12.2    10    10.9

I've tried to do this in a loop to print in a table and it works, but I would like to do it in a query so it will be faster.

Actually, my query looks like that :

SELECT IDContext, IDInput, ReadVal
FROM data
LEFT JOIN io ON io.IDIO = data.IDIO
ORDER BY IDContext, No

Is this possible to get that result in one single query?

1 Answer 1

1

You can do this with conditional aggregation:

SELECT IDContext,
       MAX(CASE WHEN no = 1 THEN data.ReadVal END) as io1,
       MAX(CASE WHEN no = 2 THEN data.ReadVal END) as io2,
       MAX(CASE WHEN no = 3 THEN data.ReadVal END) as io3
FROM data LEFT JOIN
     io
     ON io.IDIO = data.IDIO
GROUP BY IDContext
ORDER BY IDContext;
Sign up to request clarification or add additional context in comments.

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.