Is it possible reorder DB2 Zos v8 Table Row values into Columns
I h开发者_如何学编程ave data that looks like this in a table,
Item Attribute Value
---- --------- -----
cup color Red
cup size 16
cup type Plastic
Is it possible to run a query that will build a row like this,
Item color size type
---- ----- ---- ----
cup Red 20 Plastic
The table was built this way so I can add additional "Attributes" by only adding a new row. So the query needs to be able to add column based on the number of attributes an item has.
Any Ideas? Thanks
I think that you may have a poor design. May I suggest an alternative?
Item(ID, NAME, DESCRIPTION, ETC...) Attribute(ID, NAME, DESCRIPTION) ItemAttribute(ID, ItemID, AttributeID, Value)
With the above design (which may or may not be the BEST solution, but would certainly be a better one), you could simply add an ItemAttribute record for each item. You would specify the item, which attribute, and the value for that attribute. With your current design, you are attempting to represent a Many to Many relationship in a single table, which does not seem practical to me (many Items-to-many Attributes).
In conclusion, I do not think what you are asking for can be done with a static SQL statement. With your current setup, you may be able to dynamically generate a SQL statement by first selecting all records of a given item, and then checking to see which attributes you selected, and constructing you select clause accordingly.
精彩评论