Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Conceptual Structure / Procedure Problem

Author  Topic 

BaggaDonuts
Yak Posting Veteran

52 Posts

Posted - 2007-01-25 : 20:18:46
I'm curious if any of you have a solution to this problem I'm facing.

I have a table of products and a table of attributes. i can assign n number of attributes to a given product. So my table structure might look something like this:

Product Table
ProductNumber, Field1, Field2, Field3

ProductAttribute Table (bridge table -- 1 to many)
ProductNumber, AttributeId, AttributeDateTime

Attribute Table
AttributeId, AttributeCode

This is straight forward. However, this is where it gets tough. The attribute codes determine a "Status" for the product. So say some valid attribute codes are: "A", "B", "C". A or B = status1, C = status2. Also, a status can override another if its attribute date is more recent. so a product might have these codes assocated with it: A, C. I need to display query results in a grid view like this:

ProductNumber, Field1, Field2, Field3, Status
1,value, value, value, status1
2,value, value, value, status2
3,value, value, value, status1


how would you structure your tables / queries to accomplish this? Shoudl this logic be in a data view or should i return all the attributes and let the application determine the business rules? Just curious what solutions people might have.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-01-25 : 20:31:04
[code]
select p.ProductNumber, p.Field1, p.Field2, p.Field3,
a.AttributeId
from Product p inner join ProductAttribute a
on p.ProductNumber = a.ProductNumber
and a.AttributeDateTime = (select max(AttributeDateTime) from ProductAtrtibute x where x.ProductNumber = a.ProductNumber)
[/code]


KH

Go to Top of Page
   

- Advertisement -