I have the below table,
Company_ID part_id Serial NO
------------------ ----------- --------------
1 BAU BAU2512
1 BAU BAU3512
1 BAU BAU4512
2 BAU BAU5512
2 BAU BAU6512
2 BAU BAU7512
And I want a query to return this
Company_id Item#1 Item#2 Item#3
------------------ --------- ------------- -----------
1 BAU2512 BAU3512 BAU4512
2 BAU5512 BAU6512 BAU7512
any help with the query appreciated, using SQL Server 2008
To create the sample table.
-- Suppress data loading messages
SET NOCOUNT ON
-- Create Sample Data using a Table Varable
DECLARE @Company TABLE
(Company_ID int,
part_ID varchar(30),
SerialNO varchar(30))
-- Load Sample Data
INSERT INTO @Company VALUES (1, 'BAU', 'BAU2512')
INSERT INTO @Company VALUES (1, 'BAU', 'BAU3512')
INSERT INTO @Company VALUES (1, 'BAU', 'BAU4512')
INSERT INTO @Company VALUES (2, 'BAU', 'BAU5512')
INSERT INTO @Company VALUES (2, 'BAU', 'BAU6512')
INSERT INTO @Company VALUES (2, 'BAU', 'BAU7512')
Aha I think there is no way than to use dynamic sql for this. based on the answer here PIVOT in sql 2005
below is the solution for works for me.
Create tables(s) etc
The query
Answer here:
http://sqlfiddle.com/#!3/7fd86/1