Respuesta: Pivot de dos columnas dinamico eso lo puedes sacar con lo que te pase, solo piensale un poquito.....
Código SQL:
Ver originalSELECT t1.producto,MAX(t1.[201406]),MAX(t1.[201407]),MAX(t1.[201408]),MAX(t1.[201409]),MAX(t1.[201410]),MAX(t1.[201411]),MAX(t1.[201412]),MAX(t1.[201501]),MAX(t1.[201502]), MAX(t2.[201406]),MAX(t2.[201407]),MAX(t2.[201408]),MAX(t2.[201409]),MAX(t2.[201410]),MAX(t2.[201411]),MAX(t2.[201412]),MAX(t2.[201501]),MAX(t2.[201502]) FROM( SELECT producto, [201406],[201407],[201408],[201409],[201410],[201411],[201412],[201501],[201502] FROM ( SELECT producto,requerido,periodo,demanda FROM #mitabla ) AS sourcetable pivot ( MAX(requerido) FOR periodo IN ( [201406],[201407],[201408],[201409],[201410],[201411],[201412],[201501],[201502]) ) AS pivote ) AS t1 INNER JOIN ( SELECT producto, [201406],[201407],[201408],[201409],[201410],[201411],[201412],[201501],[201502] FROM ( SELECT producto,requerido,periodo,demanda FROM #mitabla ) AS sourcetable pivot ( MAX(demanda) FOR periodo IN ( [201406],[201407],[201408],[201409],[201410],[201411],[201412],[201501],[201502]) ) AS pivote ) AS t2 ON (t1.producto=t2.producto) GROUP BY t1.producto
algo asi :) y lo obtienes con algo como esto:
Código SQL:
Ver originalCREATE TABLE #mitabla( Producto VARCHAR(04), periodo INT, Requerido INT, Demanda INT ) GO INSERT INTO #mitabla(Producto, Periodo, Requerido, Demanda) VALUES('1001',201406,4480,0) INSERT INTO #mitabla(Producto, Periodo, Requerido, Demanda) VALUES('1001',201407,10973,1105) INSERT INTO #mitabla(Producto, Periodo, Requerido, Demanda) VALUES('1001',201408,8960,8960) INSERT INTO #mitabla(Producto, Periodo, Requerido, Demanda) VALUES('1001',201409,11200,11200) INSERT INTO #mitabla(Producto, Periodo, Requerido, Demanda) VALUES('1001',201410,30914,30914) INSERT INTO #mitabla(Producto, Periodo, Requerido, Demanda) VALUES('1001',201411,27602,27602) INSERT INTO #mitabla(Producto, Periodo, Requerido, Demanda) VALUES('1001',201412,19594,19594) INSERT INTO #mitabla(Producto, Periodo, Requerido, Demanda) VALUES('1001',201501,10719,10719) INSERT INTO #mitabla(Producto, Periodo, Requerido, Demanda) VALUES('1001',201502,7290,7290) INSERT INTO #mitabla(Producto, Periodo, Requerido, Demanda) VALUES('1002',201406,8521,0) INSERT INTO #mitabla(Producto, Periodo, Requerido, Demanda) VALUES('1002',201407,12786,4304) INSERT INTO #mitabla(Producto, Periodo, Requerido, Demanda) VALUES('1002',201408,8960,8960) INSERT INTO #mitabla(Producto, Periodo, Requerido, Demanda) VALUES('1002',201409,26880,26880) INSERT INTO #mitabla(Producto, Periodo, Requerido, Demanda) VALUES('1002',201410,37970,37970) INSERT INTO #mitabla(Producto, Periodo, Requerido, Demanda) VALUES('1002',201411,14754,14754) INSERT INTO #mitabla(Producto, Periodo, Requerido, Demanda) VALUES('1002',201412,11364,11364) INSERT INTO #mitabla(Producto, Periodo, Requerido, Demanda) VALUES('1002',201501,17947,17947) INSERT INTO #mitabla(Producto, Periodo, Requerido, Demanda) VALUES('1002',201502,3000,3000) SELECT DISTINCT periodo AS dato,IDENTITY(INT,1,1) AS rn INTO #temp2 FROM #mitabla SELECT * FROM #temp2 DECLARE @x INT DECLARE @query Nvarchar(MAX) DECLARE @variable Nvarchar(MAX) DECLARE @fecha Nvarchar(MAX) SET @x=1 SET @query='' SET @variable='' SET @fecha='' while @x<=(SELECT COUNT(*) FROM #temp2) BEGIN SET @fecha=@fecha + (SELECT '[' + CONVERT(VARCHAR(20),dato) + ']' FROM #temp2 WHERE rn=@x) + ',' SET @x=@x+1 END print @fecha SET @fecha=SUBSTRING(@fecha,1,len(@fecha)-1) SET @query=' select * from( select producto, ' + @fecha + ' from ( SELECT producto,requerido,periodo,demanda FROM #mitabla ) as sourcetable pivot ( max(requerido) for periodo in ( ' + @fecha + ') ) as pivote ) as t1 inner join ( select producto, ' + @fecha + ' from ( SELECT producto,requerido,periodo,demanda FROM #mitabla ) as sourcetable pivot ( max(demanda) for periodo in ( ' + @fecha + ') ) as pivote ) as t2 on (t1.producto0t2.producto) ) ' print @query EXEC sp_executesql @query DROP TABLE #temp2
__________________ What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me |