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
LVL 8
Razzie_Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
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).
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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...
0
Razzie_Author Commented:
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
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Guy Hengel [angelIII / a3]Billing EngineerCommented:
ok, here we go:
select o.ID , o.Name 
, max(case when p.propertyname = 'Property1' then p.propertyvalue else null end) property1
, max(case when p.propertyname = 'Property2' then p.propertyvalue else null end) property2
from objects o
left join objectproperties p
  on o.id = p.objectid
group by o.id, o.name

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Razzie_Author Commented:
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
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.