I have a view like this one
id name characteristic value isList
1 cube sides 6 0
1 cube color blue 0
2 triangle sides 3 0
3 hexagon (null) (null) (null)
4 rectangle weight 15 0
I need to select all ids and names and retrieve some characteristics and the respective value. for instance, I want to retrieve all figures (ids 1, 2, 3 and 4) and the characteristics sides and color (if are available, if not, only id and name are filled up; the others are null).
I tried
select *
from shapes_view
where (id = 1 or id = 2 or id = 3 or id = 4) and (characteristic like 'sides' or characteristic like 'color')
but it, obviously, retrieves the ids 1 and 2 but not 3 and 4.
My guess is that I need some sort of subquery to do this but when I try to join this view with itself, I get a long list of combinations that aren’t anywhere near of what I need.
What i intend to get is something like
id name characteristic value isList
1 cube sides 6 0
1 cube color blue 0
2 triangle sides 3 0
3 hexagon (null) (null) (null)
4 rectangle (null) (null) (null)
I know I can select all values and exclude what I do not what in the java side, but it doesn’t sound very correct…
Can anyone help me on this one?
Best regards
You can use a self join– actually, a couple of self joins
Or you can pivot the data
The complexity of the queries required to extract data for N different attributes is one of the reasons that this sort of very generic entity-attribute-value data model is generally frowned upon.