Hope you can help... I have data table in this format (Lets refer this table as 'Product')
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)*
I would need the data in this format:
productid type name cost
p0001 p1 Premium 172.00
p0002 p3 standard 13.00
I tried with a function and a view to get this format but it takes good few mins to get some 1000 records. Wonder if anyone knows quicker way?
What I tried:
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 productid=@productid and property_name='type';
select @name=property_value from product where productid=@productid and property_name='name';
select @cost=property_value from product where productid=@productid and property_name='cost';
if isnull(@productid,'')<>''
begin
insert @retdetails
select @type, @name, @cost;
end;
return;
end;
then a view
select p.productid, pd.type, pd.name, pd.cost
from (select distinct productid from product) p
cross apply dbo.fun1(p.productid) pd
The slower response might be down to 'distinct' but without that I get duplicate records. I would appreciate any suggestion to get a quickier sql response.
Many Thanks