sql添加其他字段

时间:2013-02-07 19:20:27

标签: sql sql-server-2008 tsql

我得到了用户输入测试数据的要求 我有字段:

例如

 Blood   Pulse 
 ----    -----
 12      13

需要添加另一个字段,我们不知道测试的内容。 我在考虑有一个名为Misc1Label,Misc1Value

的字段

假设 Misc1Label 是口头且 Misc1Value 是88

稍后我可以使用Pivot,如果我们需要捕获Misc1Label的值并使用Pivot以便我可以有类似的东西

 Blood  Pulse  Oral
 -----  ----   ----
   12    13     88

我在想是否还有其他最好的处理方法。

提前致谢

1 个答案:

答案 0 :(得分:1)

你真的需要将Blood和Pulse作为单独的字段保存吗?您可以将Blood和Pulse视为不同的属性。听起来像这样的东西会起作用,并且无需使用PIVOT命令就可以轻松查询。

PersonAttribute
    PersonId int (I'm only presuming here)
    AttributeId int

Attribute
    AttributeId int
    AttributeTypeId int
    AttibuteValue varchar(100)

AttributeType
    AttributeTypeId int
    AttributeType varchar(100)

然后,您可以在您的AttributeType表中存储Blood,Pulse,Weight等,并使用PersonAttribute表作为主表的1-N方法。只是一个想法。

SELECT Distinct PA.PersonId
FROM PersonAttribute PA
   INNER JOIN Attribute A ON PA.AttributeId = A.AttributeId 
   INNER JOIN AttributeType AT ON A.AttributeTypeId = AT.AttributeTypeId 
WHERE AT.AttributeType = 'Blood'

当然,如果需要,可以应用相同的模型将血液和脉冲留在主表中。

祝你好运。