Link to home
Start Free TrialLog in
Avatar of Razzie_
Razzie_

asked on

SQL query: Append colums to resultset based on dynamic data

Hey all,

Say I have a database table (MySql for now) named 'objects', which is a very basic table:

ID - Name
========

Now I also have a table 'objectproperties':

ID - ObjectID - PropertyName - PropertyValue
====================================

Objectproperties can contain an unknown number of name value pairs which are additional properties for an object. So let's say there is an object id 33 with the name 'Test'. In objectproperties two properties are defined:

1 - 33 - Property1 - Value1
2 - 33 - Property2 - Value2

Now what I am trying to accomplish, is writing a query that produces the following result:

ObjectID - Name - Property1 - Property2
================================
    33         Test       Value1        Value2

So the value of the column 'propertyname' in objectproperties should be appended as the column name of the resultset, with the corresponding propertyvalue as its value.

I've searched and found things like dynamic cross-tab sql, but I'm not sure if that is the way to go, if it can be done easier, etcetera. Help is really appreciated. Thanks in advance!

Razzie

(attached is the create table scripts if necessary)
(I accidently ranked myself as guru on databases, I'm not, just intermediate or something)
sql.txt
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

yes, crosstable or pivot is the keyword
no, mysql does not have this functionality, and the procedure(s) you found are quite sure for MS SQL server and not for MySQL...

so, in short, you cannot do this with pure mysql (yet).
note: if the values of PropertyName are "limited", you can do it with a group by...
tell me if that could be the way you want to go...
Avatar of Razzie_
Razzie_

ASKER

Hello angelIII,

Thanks for your reply. It depends on what you mean if the values are limited.
Basically, they are not really limited. There can be an x number of properties.

However, for a single given type of object, I DO know what properties to expect (from C# code).
So, in my code I know which type of object with ID 33 is, and I do know all the properties from the table it will have. So I guess the idea you have in mind is therefor possible (at least I hope I do!)

Regards.
Razzie
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Razzie_

ASKER

Thanks a lot angelll! That will sure do the trick.

Could you please explain in short the max(case ...) syntax? I noticed that when I omit max, I only get the value for the first property. But I can't see why. Or a link where it is explained, I searched on Google but couldn't find :-)

Thanks again!
Razzie
run this and look at the data returned:
select o.ID , o.Name 
, case when p.propertyname = 'Property1' then p.propertyvalue else null end property1
, case when p.propertyname = 'Property2' then p.propertyvalue else null end property2
from objects o
left join objectproperties p
  on o.id = p.objectid
order by o.id, o.name

Open in new window