0

我有经典的人-> 人属性方案。所以,像这样:person(PK) <- person_attribute(FK)

我需要的是一个查询来获取一个人与她的属性相结合的一行。例如,要转换:

Person:
{ ID = 123456, Name = 'John Smith', Age = 25 }
Attributes:
1) { PersonID = 123456, AttributeTypeID = 'Height', AttributeValue = '6'6'''}
2) { PersonID = 123456, AttributeTypeID = 'Weight', AttributeValue = '220lbs'}
3) { PersonID = 123456, AttributeTypeID = 'EyeColor', AttributeValue = 'Blue'}

到:

PersonWithAttributes
{
 ID = 123456, Name = 'John Smith', Age = 25, Height = '6'6''', Weight = '220lbs', EyeColor = 'Blue'
}

更糟糕的是,我的人在一个表变量中。

所以,我有(在一个参数为 person_id 的 sp 中):

--result table
declare @people_info table
(
person_id int,
name nvarchar(max),
age int,
height nvarchar(10) null,
weight nvarchar(10) null,
eye_color nvarchar(16) null
)


insert into @people_info
 select person_id, name, age, null, null, null
  from dbo.HR.people where person_id = @person_id

update pi
 set
  pi.height = (select pa.attribute_value where pa.attribute_type_id = 'Height'),
  pi.height = (select pa.attribute_value where pa.attribute_type_id = 'Weight'),
  pi.eye_color = (select pa.attribute_value where pa.attribute_type_id = 'EyeColor')
 from
  @people_info pi
   inner join dbo.HR.person_attributes pa on pi.person_id = pa.person_id

select * from @people_info

由于某种原因,这当然不起作用。如果我查询两个连接表并选择“pa.attribute_value where pa.attribute_type_id = 'someval'”,我会得到正确的值。但是更新不起作用。

当然,我可以将其写为三个更新,但我认为执行一个连接然后在更新子句中进行过滤会更快。

另外,请记住,我的属性分布在三个表中,而不仅仅是属性表。所以,这就是我有 table 变量的原因。

非常欢迎任何帮助。也许我正在以错误的方式解决这个问题。性能很重要。实现这一目标的最有效方法是什么?

非常感谢你。

4

2 回答 2

1

也许你想要类似的东西:

update pi 
 set 
  pi.height = paH.attribute_value,
  pi.weight = paW.attribute_value,
  pi.eye_color = paE.attribute_value
 from 
  @people_info pi 
   inner join dbo.HR.person_attributes paH on pi.person_id = paH.person_id 
                                            and paH.attribute_type_id = 'Height'
   inner join dbo.HR.person_attributes paW on pi.person_id = paW.person_id 
                                            and paW.attribute_type_id = 'Weight'
   inner join dbo.HR.person_attributes paE on pi.person_id = paE.person_id 
                                            and paE.attribute_type_id = 'EyeColor'
于 2012-10-22T18:30:03.500 回答
1

尝试使用以下代码进行更新:

    update 
        pi
    set
        pi.height = pa.Height
        pi.weight = pa.Weight
        pi.eye_color = pa.EyeColor
    from
        @people_info pi
    inner join 
        (
            SELECT
                 person_id
                ,[Height] Height
                ,[Weight] Weight
                ,[EyeColor] EyeColor
            FROM
            (
                SELECT
                      attribute_type_id
                    , attribute_value
                    , person_id   
                FROM
                    dbo.HR.person_attributes pa
            ) pa
            PIVOT
            (
                MAX(attribute_value) FOR attribute_type_id IN ([Height],[Weight],[EyeColor])
            )pvt

        ) pa
    on 
        pi.person_id = pa.person_id
于 2012-10-23T08:22:58.100 回答