Friday, February 17, 2012

Dynamic Column Name - Hard Problem...

Ok, here is my problem, lets say i have the following three tables

Product_Group_Master
ProductGroupId

Size_Groups
SizeGroupCode
SizeCode1
SizeCode2
SizeCode3
SizeCode4
SizeCode5

Product_Group_Inventory
ProductGroupId
Inventory1
Inventory2
Inventory3
Inventory4
Inventory5

Individuale_Products
ProductId
ProductGroupId
SizeGroupCode
SizeCode

Ok, now that you understant the table layout... i need get a list of ProductId's with there respective inventory count

So what i need to do is select all the product id's from the "Individuale_Products" table, and the based apon there "SizeGroupCode", and "SizeCode"...
I need to look up in the "Size_Groups" table...
on the row that is equal to SizeGroupCode...
the colum name that has the value of the "SizeCode"...
So that then i can look up in the "Product_Group_Inventory" table the correct inventory column.

Please feel free to ASK any furthur Questions... or e-mail them to me at gregmoser@.forwarddev.comcan you post some example (with data) of you scenario and what results do you want?|||You should consider remaking the tables:

Size_Groups
SizeGroupCode
CodeNum
Value
Product_Group_Inventory
ProductGroupId
InvNum
Value

That way you can make easier sql statments :

select Product_Group_Inventory.value
from size_groups inner join Individuale_Products
on size_groups.sizegroupcode = Individuale_Products.SizeGroupCode
and size_groups.CodeNum = Individuale_Products.SizeCode
inner join Product_Group_Inventory
on Product_Group_Inventory.ProductGroupId = Individuale_Products.ProductGroupId
and Product_Group_Inventory.InvNum = size_groups.value

As it stands your best bet is making dynamic SQL statemnts and execute them using sp_executesql.

No comments:

Post a Comment