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.
| Author |
Topic |
|
ravishwor
Starting Member
20 Posts |
Posted - 2008-02-03 : 22:40:10
|
| I know this may have been asked before but can someone pls hel mw out here. i even tried to use the Crosstab SP that i found out on this site but it is not for what i need.I have a Table/View called [Shipment] with the data below.ShipNo Supplier=================1 CFA1 TFA2 LRA2 LRB3 ABC4 TFAI want the following as my result.ShipNo Supplier1 Supplier2==========================1 CFA TFA2 LRA LRB3 AB4 TFA Thx.Rav |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-03 : 23:27:49
|
| [code]DECLARE @temp table(ShipNo int, Supplier varchar(10))INSERT INTO @temp VALUES (1,'CFA')INSERT INTO @temp VALUES (1, 'TFA')INSERT INTO @temp VALUES (2, 'LRA')INSERT INTO @temp VALUES (2, 'LRB')INSERT INTO @temp VALUES (3, 'ABC')INSERT INTO @temp VALUES (4, 'TFA');WITH Your_CTE AS(SELECT ROW_NUMBER() OVER (PARTITION BY ShipNo ORDER BY Supplier) AS RowNo,ShipNo,SupplierFROM @temp)SELECT t1.ShipNo,t1.Supplier,ISNULL(t2.Supplier,'')FROM Your_CTE t1OUTER APPLy(SELECT Supplier FROM Your_CTE WHERE ShipNo=t1.ShipNo AND RowNo=2) t2WHERE t1.RowNo=1output---------------------------ShipNo Supplier1 Supplier2----------- ---------- ----------1 CFA TFA2 LRA LRB3 ABC 4 TFA [/code] |
 |
|
|
ravishwor
Starting Member
20 Posts |
Posted - 2008-02-03 : 23:43:43
|
| Thx a lot visakh16,can the Columns be made dynamic, ie,ShipNo Supplier=================1 CFA1 TFA2 LRA2 LRB3 ABC4 TFA1 ABCShipNo Supplier1 Supplier2 Supplier3====================================1 CFA TFA ABC2 LRA LRB3 AB4 TFAThx a lot. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-04 : 00:45:09
|
You need to use dynamic SQL to achieve this:-CREATE table #temp (ShipNo int, Supplier varchar(10))INSERT INTO #temp VALUES (1,'CFA')INSERT INTO #temp VALUES (1, 'TFA')INSERT INTO #temp VALUES (2, 'LRA')INSERT INTO #temp VALUES (2, 'LRB')INSERT INTO #temp VALUES (3, 'ABC')INSERT INTO #temp VALUES (4, 'TFA')INSERT INTO #temp VALUES (2, 'KKL')INSERT INTO #temp VALUES (2, 'MMC')INSERT INTO #temp VALUES (4, 'IIF')INSERT INTO #temp VALUES (1, 'TTK')DECLARE @ShipList varchar(2000)SELECT @ShipList = LEFT(sl.ShipList,LEN(sl.ShipList)-1)FROM (SELECT DISTINCT 'Supplier' + CAST(ROW_NUMBER() OVER (PARTITION BY ShipNo ORDER BY Supplier) AS varchar(3)) + ',' AS [text()] FROM #temp FOR XML PATH(''))sl(ShipList)DECLARE @sql varchar(max)SET @sql='SELECT *FROM(SELECT ShipNo, Supplier, ''Supplier'' + CAST(ROW_NUMBER() OVER (PARTITION BY ShipNo ORDER BY Supplier) AS varchar(3)) AS SupplierNoFROM #temp)dPIVOT(MAX(d.Supplier)FOR d.SupplierNo in (['+ REPLACE(@ShipList,',','],[') + ']))p' EXEC (@Sql)output-------------------------------ShipNo Supplier1 Supplier2 Supplier3 Supplier4----------- ---------- ---------- ---------- ----------1 CFA TFA TTK NULL2 KKL LRA LRB MMC3 ABC NULL NULL NULL4 IIF TFA NULL NULL |
 |
|
|
ravishwor
Starting Member
20 Posts |
Posted - 2008-02-05 : 00:20:12
|
| Thx a lot visakh16. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-05 : 00:38:21
|
| You are welcome ravishwor. Feel free to post whenever you have another doubt. |
 |
|
|
ravishwor
Starting Member
20 Posts |
Posted - 2008-03-31 : 01:33:47
|
| Hi Visakh16,i get an error when i try to run this? can u pls help me? something to do with the quotes that i cannot figure out!ERROR MSG:Msg 207, Level 16, State 1, Line 9Invalid column name 'Supplier'.DECLARE @SupplierList varchar(2000)SELECT @SupplierList = LEFT(sl.SupplierList,LEN(sl.SupplierList)-1)FROM (SELECT DISTINCT 'Supplier' + CAST(ROW_NUMBER() OVER (PARTITION BY stock_code ORDER BY cre_accountcode) AS varchar(20)) + ',' AS [text()] FROM stock_supplier FOR XML PATH(''))sl(SupplierList)DECLARE @sql varchar(max)SET @sql='SELECT *FROM(SELECT Stock_code, cre_accountcode, ''Supplier'' + CAST(ROW_NUMBER() OVER (PARTITION BY Stock_code ORDER BY cre_accountcode) AS varchar(20)) AS SupplierNoFROM stock_supplier) dPIVOT(MAX(d.Supplier)FOR d.SupplierNo in (['+ REPLACE(@SupplierList,',','],[') + ']))p' EXEC (@Sql)Thx, |
 |
|
|
|
|
|
|
|