Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following is <em>extremely</em> rough but it should get the point across. It normalizes the data and moves towards a semi-object oriented (attribute/value/attribute value) structure. I'll leave it up to you to reinforce referential integrity, but the following is flexible and will return the results you want:</p> <pre><code>CREATE TABLE Person ( PersonID INT PRIMARY KEY IDENTITY(1,1) ,Name NVARCHAR(255) ); GO CREATE TABLE PersonAttribute ( PersonID INT ,CondAttributeID INT ,Value NVARCHAR(255) ); GO CREATE TABLE CondAttribute ( AttributeID INT PRIMARY KEY IDENTITY(1,1) ,Attribute NVARCHAR(255)); GO CREATE TABLE CondTable ( CondTableID INT PRIMARY KEY IDENTITY(1,1) ,CondAttributeID INT ,StartValue MONEY ,EndValue MONEY ); GO CREATE TABLE CalculationValues ( CalculationID INT PRIMARY KEY IDENTITY(1,1) ,CalculationValue INT ); GO CREATE TABLE CondCalculation ( CondTableID INT ,CalculationID INT ); INSERT Person (Name) VALUES ('Joe') ,('Bob') ,('Tom'); INSERT PersonAttribute ( PersonID ,CondAttributeID ,Value ) VALUES (1, 1, '60') ,(1, 2, '6.2') ,(1, 3, '169') ,(2, 1, '15') ,(2, 2, '5.1') ,(2, 3, '100') ,(3, 1, '10') ,(3, 2, '4.5') ,(3, 3, '50'); INSERT CondAttribute (Attribute) VALUES ('Age') ,('height') ,('weight'); INSERT CondTable ( CondAttributeID ,StartValue ,EndValue) VALUES (1,18,999) --Age ,(2,6,99) --Height ,(2,0,5.99) -- Height ,(1,0,17) -- Age ,(3,60,999) -- Weight ,(3,0,59); -- Weight INSERT CalculationValues (CalculationValue) VALUES (5) ,(10) ,(15) ,(20); INSERT CondCalculation (CondTableID, CalculationID) VALUES (1,4) ,(2,4) ,(1,3) ,(3,3) ,(4,2) ,(5,2) ,(5,1) ,(6,1); SELECT * FROM Person AS p JOIN PersonAttribute AS pa ON p.PersonID = pa.PersonID JOIN CondAttribute AS ca ON pa.CondAttributeID = ca.AttributeID JOIN CondTable AS ct ON ca.AttributeID = ct.CondAttributeID AND CONVERT(money,pa.Value) BETWEEN ct.StartValue AND ct.EndValue JOIN CondCalculation AS cc ON cc.CondTableID = ct.CondTableID JOIN CalculationValues AS c ON cc.CalculationID = c.CalculationID WHERE p.PersonID = 1 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload