1

here is how looks like my table:

fid  | section  | baseData
59   |    58    |  somedata
59   |    58    |  somedata
60   |    58    |  somedata

I need to get data like this:

owner, name, email

All values are in table baseData, but owner is fid=59, name is fid 70, email is fid 71

So I would like to get:

 owner       |  name       |  email
 owner_name  |  user_name  | user_email
 owner_name2 |  user_name2 | user_email2

I'm trying in this way:

SELECT fid, 
(CASE 
WHEN (fid = 59 AND section = 58)
THEN baseData
END) AS owner
FROM sobipro_field_data
WHERE section = 58 GROUP BY fid

but getting only nulls

what I'm trying to get is:

When fid = 59 -> select baseData as owner
When fid = 70 -> select baseData as name
When fid = 71 -> select baseData as email
3
  • 3
    I have no clue how the first table and last SQL relate to your desired output. Commented Nov 28, 2014 at 11:00
  • 1
    I read this few times but really did not understand what you really are looking at Commented Nov 28, 2014 at 11:01
  • Do you mean you're using one column (fid) to associate three columns (owner, name and email)? Commented Nov 28, 2014 at 11:03

2 Answers 2

2

For one section:

SELECT  
    max(CASE WHEN (fid = 59) THEN baseData ELSE null END) AS owner,  
    max(CASE WHEN (fid = 70) THEN baseData ELSE null END) AS name,  
    max(CASE WHEN (fid = 71) THEN baseData ELSE null END) AS email  
FROM sobipro_field_data  
WHERE section = 58;

For each section:

SELECT section,
    max(CASE WHEN (fid = 59) THEN baseData ELSE null END) AS owner,  
    max(CASE WHEN (fid = 70) THEN baseData ELSE null END) AS name,  
    max(CASE WHEN (fid = 71) THEN baseData ELSE null END) AS email  
FROM sobipro_field_data  
group by section;
Sign up to request clarification or add additional context in comments.

Comments

0
SELECT  
(CASE WHEN (fid = 59 AND section = 58) THEN baseData END) AS owner,  
(CASE WHEN (fid = 70 AND section = 58) THEN baseData END) AS name,  
(CASE WHEN (fid = 71 AND section = 58) THEN baseData END) AS email  

FROM sobipro_field_data  

Cheers!

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.