Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Permutation & Combination in sql server

Author  Topic 

chandanp
Starting Member

1 Post

Posted - 2011-12-12 : 02:07:14
These 2 tables are related with ProductID Column

PartID Values ProductID
1 ab 11
1 bc 11
2 bc 11
2 ef 11
3 hi 11
3 hy 11

ProductId Name
11 TestProd

These are 2 table and i want result as follows

ProductID Name Part1 Part2 Part3
11 TestProd/ab(1)/bc(2)/hi(3) 1.1 2.1 3.1
11 TestProd/ab(1)/bc(2)/hy(3) 1.1 2.1 3.2
11 TestProd/ab(1)/ef(2)/hi(3) 1.1 2.2 3.1
11 TestProd/ab(1)/ef(2)/hy(3) 1.1 2.2 3.2
11 TestProd/bc(1)/bc(2)/hi(3) 1.2 2.1 3.1
11 TestProd/bc(1)/bc(2)/hy(3) 1.2 2.1 3.2
11 TestProd/bc(1)/ef(2)/hi(3) 1.2 2.2 3.1
11 TestProd/bc(1)/ef(2)/hy(3) 1.2 2.2 3.2

Or

ProductID Name Part1 Part2 Part3
11 TestProd/ab(1)/bc(2)/hi(3) ab bc hi
11 TestProd/ab(1)/bc(2)/hy(3) ab bc hy
11 TestProd/ab(1)/ef(2)/hi(3) ab ef hi
11 TestProd/ab(1)/ef(2)/hy(3) ab ef hy
11 TestProd/bc(1)/bc(2)/hi(3) bc bc hi
11 TestProd/bc(1)/bc(2)/hy(3) bc bc hy
11 TestProd/bc(1)/ef(2)/hi(3) bc ef hi
11 TestProd/bc(1)/ef(2)/hy(3) bc ef hy


Can any one please help me,
i want to perform above task with sql query in sqlserver2008
Thanks.....

Chandan

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-12 : 10:05:49
something like below


SELECT t2.ProductID,
t2.Name + '/'+ [Values1]+'(' + CAST(Part1ID AS varchar(5)) + ')'+
'/'+ [Values2]+'(' + CAST(Part2ID AS varchar(5)) + ')'+
'/'+ [Values3]+'(' + CAST(Part3ID AS varchar(5)) + ')' AS Name,
[Values1] AS Part1,
[Values2] AS Part2,
[Values3] AS Part3
FROM table2 t2
CROSS APPLY (SELECT m.PartID AS part1ID,m.Values AS Values1,n.PartID AS Part2ID,n.Values AS Values2,o.PartID AS Part3ID,o.Values AS Values3
FROM (SELECT ROW_NUMBER() OVER (ORDER BY [Values]) AS rn,PartID ,Values
FROM table1
WHERE PartID =1)m
CROSS JOIN (SELECT ROW_NUMBER() OVER (ORDER BY [Values]) AS rn,PartID ,Values
FROM table1
WHERE PartID =2)n
CROSS JOIN (SELECT ROW_NUMBER() OVER (ORDER BY [Values]) AS rn,PartID ,Values
FROM table1
WHERE PartID =3)o
)t1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -