0

I have a SQL query problem with the following abstract sample context: There are 2 different input data for my sql query defined as ''MainElement'' with key 123 for the one and 789 for the other main element.

Further I have a table called Relation with columns pk, FirstElement, SecondElement and ThirdElement.

Furthermore there is a table called Props with the columns pk, name and valueString. The special feature about this context is that column name in Props defines 2 further columns called 4thElement and 5thElementof table Relation as a row with its values in column valueString .

|pk | 1stElement | 2ndElement | 3rdElement |
|abc|-----123----|-----456----|-----null---|
|def|-----789----|-----101112-|---131415---|

|Pk | Name | ValueString |
|def|4thElement|161718---|
|def|5thElement|ghi------|

As you can see the MainElement 789 has a value for 4thElement and 5thElement in Props, but MainElement 123 hasn't any value in Props.

What I need is an universal SQL query with input value 1stElement e.g., 123 or 789 that returns me a result for both main elements independent of the fact that MainElement 123 hasn't any value in Props.

Sample result:

|1stElement | 2ndElement | 3rdElement | 4thElement | 5thElement |
|123--------|------456---|-----null---|---null-----|----null----|
|789--------|----101112--|---131415---|---161718---|----ghi-----|

I am using Oracle SQL Developer.

Select 
rel.1stElement,
....
From 
Relation rel,
Props pro,
Where 
?

Thanks in advance.

3
  • You say that input value will be "123 or 789", but then want to have "result for both main elements". Isn't it a self-contradictory? Please clarify. Commented Apr 17, 2013 at 14:27
  • I don't understand why this should be self-contradictory? I want to use this sql query in general for both inputs. First sql query with input 123 and secondly with 789 or vice versa. Not both simultaneously for an execution. The problem is that I get an empty result when I have input 123 because in table Props I haven't any entry with pk abc ;-))) Commented Apr 18, 2013 at 7:48
  • You query outline hints at INNER JOIN that eliminates any NULL values. OUTER JOIN should take care of it. It's not clear if you can have multiple values of 4thElement and 5thElement for the same pk. In either way, it seems that the already suggested answer would do the job. If it does not, you will need to clarify your requirements. Commented Apr 18, 2013 at 16:39

1 Answer 1

1

This should do the work, this is typically a pivot query that you need:

SELECT rel.pk, rel.1stElement, rel.2ndElement, rel.3rdElement 
     , MAX(CASE WHEN pro.Name = '4thElement' 
           THEN pro.ValueString 
           ELSE NULL
           END) as 4thElement
     , MAX(CASE WHEN pro.Name = '5thElement' 
           THEN pro.ValueString
           ELSE NULL
           END) as 5thElement
FROM Relation rel 
    LEFT OUTER JOIN Props pro 
        ON rel.pk = pro.pk
GROUP BY rel.pk, rel.1stElement, rel.2ndElement, rel.3rdElement
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.