希望你能帮助... 我有数据表格式(让参照此表作为“产品”)SQL服务器 - 多行成一个
productid property_name property_value last_updated
p0001 type p1 05-Oct-2010
p0001 name Premium 05-Oct-2010
p0001 cost 172.00 05-Oct-2010
p0002 type p3 06-Oct-2010
p0002 name standard 06-Oct-2010
p0002 cost 13.00 06-Oct-2010
*(there are like 50 more properties of which i would need 15 atleast in my query.
However, i just ignore them for this example)*
我需要在这个格式的数据:
productid type name cost
p0001 p1 Premium 172.00
p0002 p3 standard 13.00
我试着用一个函数和视图来获得这种格式,但它需要好几分钟才能获得1000条记录。想知道如果有人知道更快的方式?
我试了一下:
Create function fun1(@productid nvarchar(50)) returns @retdetails table
(
type nvarchar(50) null,
name nvarchar(50) null,
cost nvarchar(50) null,
)
begin
declare
@type nvarchar(50),
@name nvarchar(50),
@cost nvarchar(50),
select @type=property_value from product where [email protected] and property_name='type';
select @name=property_value from product where [email protected] and property_name='name';
select @cost=property_value from product where [email protected] and property_name='cost';
if isnull(@productid,'')<>''
begin
insert @retdetails
select @type, @name, @cost;
end;
return;
end;
然后视图
select p.productid, pd.type, pd.name, pd.cost
from (select distinct productid from product) p
cross apply dbo.fun1(p.productid) pd
较慢的响应可能会下调至“独立的”,但没有,我得到重复的记录。我将不胜感激任何建议,以获得更快的SQL响应。
非常感谢
正在改变表结构的选项呢? – Purplegoldfish
谢谢你的快速回复。我没有自己的表和其刷新的一些外部对象,所以不能改变表(不知道为什么它被设计在第一个地方,可能是为了方便添加新的属性)。尝试自我加入,超过15个属性。由于波格丹,我去了更容易的选择(我不明白枢轴:))) – user981785