[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 474
  • Last Modified:

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
0
Razzie_
Asked:
Razzie_
  • 4
  • 2
1 Solution
 
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now