Hi,
Please try the below query. If your requirment is to show only fix number of columns for producttype,productname,price (as per below example 3 producttype,productname,price), then you can use the below query. Please note that in <select shop, producttype,
productname, price, ROW_NUMBER() over(partition by shop order by producttype) as seq>, the seq field is extracted to get the running number within the shop and the seq field is used to generate the column sets.
select shop, max(CASE WHEN seq='1' THEN ProductType END) as [1],max(CASE WHEN seq='1' THEN ProductName END) as [1],max(CASE WHEN seq='1' THEN price END) as [1],
max(CASE WHEN seq='2' THEN ProductType END) as [2],max(CASE WHEN seq='2' THEN ProductName END) as [2], max(CASE WHEN seq='2' THEN price END) as [2],
max(CASE WHEN seq='3' THEN ProductType END) as [3],max(CASE WHEN seq='3' THEN ProductName END) as [3],max(CASE WHEN seq='3' THEN price END) as [3]
from (select shop,ProductType,ProductName,price,ROW_NUMBER() over(partition by shop order by ProductType) as seq from prodsforpivot) A GROUP BY shop
To dynamically generate the columns based on tables' contents, you can use an excellent stored procedure (dynamic cross tab) written by Jeff at
http://weblogs.sqlteam.com/jeffs/archive/2005/05/02/4842.aspx
Please check if this helps and feel free to discuss should any further help required.
Mohamed Irshad