Tengo un problema con una query que anteriormente libras me estaba ayudando a resolver pero creo que plantie mal mi problema y aqui voy de nuez...
Tengo la siguiente consulta:
Código SQL:
[IMG]imgeek.net/imagepost/Myqr1.PNG[/IMG]Ver original
SELECT TRIM,Corte1,UMC1,SUM(Ton1)Ton1,Descripcion,Id_Contenedor,Ancho,UAncho,TrimCorte,Peso,Rollos FROM( SELECT DISTINCT TRIM,Corte1,UMC1,Ton1 FROM protri01 WHERE PedPipsa1 = '2073713' UNION SELECT DISTINCT TRIM,Corte2,UMC2,Ton2 FROM protri01 WHERE PedPipsa2 ='2073713' UNION SELECT DISTINCT TRIM,Corte3,UMC3,Ton3 FROM protri01 WHERE PedPipsa3 = '2073713' UNION SELECT DISTINCT TRIM,Corte4,UMC4,Ton4 FROM protri01 WHERE PedPipsa4 ='2073713' UNION SELECT DISTINCT TRIM,Corte5,UMC5,Ton5 FROM protri01 WHERE PedPipsa5 = '2073713' UNION SELECT DISTINCT TRIM,Corte6,UMC6,Ton6 FROM protri01 WHERE PedPipsa6 ='2073713' UNION SELECT DISTINCT TRIM,Corte7,UMC7,Ton7 FROM protri01 WHERE PedPipsa7 = '2073713' UNION SELECT DISTINCT TRIM,Corte8,UMC8,Ton8 FROM protri01 WHERE PedPipsa7 = '2073713' --union SELECT DISTINCT Trim,Corte8,UMC8,case when Corte8=Corte9 then Ton8+Ton9 else Ton8 end AS Ton8 FROM protri01 WHERE PedPipsa8 ='2070713' UNION SELECT DISTINCT TRIM,Corte9,UMC9,Ton9 FROM protri01 WHERE PedPipsa9 = '2073713' UNION SELECT DISTINCT TRIM,Corte10,UMC10,Ton10 FROM protri01 WHERE PedPipsa10 ='2073713' UNION SELECT DISTINCT TRIM,Corte11,UMC11,Ton11 FROM protri01 WHERE PedPipsa11 = '2073713' UNION SELECT DISTINCT TRIM,Corte12,UMC12,Ton12 FROM protri01 WHERE PedPipsa12 ='2073713' ) C INNER JOIN (SELECT DISTINCT Descripcion,Id_Contenedor,Ancho,UAncho,TRIM AS TrimCorte,SUM(Peso) Peso ,COUNT(Id_Lote) Rollos FROM prolot01 A INNER JOIN catpap01 B ON B.Id_Papel = A.Id_Papel WHERE PedidoPIPSA= '2073713'AND Peso > 0 AND DevProd = 0 AND Jagenberg = 0 AND Detenido = 0 AND Rechazado = 0 GROUP BY Id_Contenedor,Ancho,UAncho,A.TRIM,B.Descripcion)D ON C.TRIM = D.TrimCorte AND Corte1 = Ancho AND UMC1 = UAncho GROUP BY TRIM,Corte1,UMC1,Descripcion,Id_Contenedor,Ancho,UAncho,TrimCorte,Peso,Rollos
En la imagen se ve en la primera fila:
que tengo: Corte1 71 CM de 11 Toneladas.
Pero si ejecuto lo siguiente:
Se ve que para corte 1 2 y 3 tengo lo mismo, es decir tengo 3 cortes de 71 con 11 toneladas cada uno lo cual da una suma de 33 toneladas. Estamos?
Entonces intentando resolver esto, modifique la query asi:
Código SQL:
Ver original
SELECT MAX(RowN),TRIM,Corte1,UMC1,SUM(Ton1)Ton1 FROM( SELECT DISTINCT 1 RowN, TRIM,Corte1,UMC1,Ton1 FROM protri01 WHERE PedPipsa1 = '2073713' UNION SELECT DISTINCT 2 RowN,TRIM,Corte2,UMC2,Ton2 FROM protri01 WHERE PedPipsa2 ='2073713' UNION SELECT DISTINCT 3 RowN,TRIM,Corte3,UMC3,Ton3 FROM protri01 WHERE PedPipsa3 = '2073713' UNION SELECT DISTINCT 4 RowN,TRIM,Corte4,UMC4,Ton4 FROM protri01 WHERE PedPipsa4 ='2073713' UNION SELECT DISTINCT 5 RowN,TRIM,Corte5,UMC5,Ton5 FROM protri01 WHERE PedPipsa5 = '2073713' UNION SELECT DISTINCT 6 RowN,TRIM,Corte6,UMC6,Ton6 FROM protri01 WHERE PedPipsa6 ='2073713' UNION SELECT DISTINCT 7 RowN,TRIM,Corte7,UMC7,Ton7 FROM protri01 WHERE PedPipsa7 = '2073713' UNION SELECT DISTINCT 8 RowN,TRIM,Corte8,UMC8,Ton8 FROM protri01 WHERE PedPipsa8 ='2073713' UNION SELECT DISTINCT 9 RowN,TRIM,Corte9,UMC9,Ton9 FROM protri01 WHERE PedPipsa9 = '2073713' UNION SELECT DISTINCT 10 RowN,TRIM,Corte10,UMC10,Ton10 FROM protri01 WHERE PedPipsa10 ='2073713' UNION SELECT DISTINCT 11 RowN,TRIM,Corte11,UMC11,Ton11 FROM protri01 WHERE PedPipsa11 = '2073713' UNION SELECT DISTINCT 12 RowN,TRIM,Corte12,UMC12,Ton12 FROM protri01 WHERE PedPipsa12 ='2073713' ) C INNER JOIN (SELECT DISTINCT Descripcion,Id_Contenedor,Ancho,UAncho,TRIM AS TrimCorte,SUM(Peso) Peso ,COUNT(Id_Lote) Rollos FROM prolot01 A INNER JOIN catpap01 B ON B.Id_Papel = A.Id_Papel WHERE PedidoPIPSA= '2073713'AND Peso > 0 AND DevProd = 0 AND Jagenberg = 0 AND Detenido = 0 AND Rechazado = 0 GROUP BY Id_Contenedor,Ancho,UAncho,A.TRIM,B.Descripcion)D ON C.TRIM = D.TrimCorte AND Corte1 = Ancho AND UMC1 = UAncho GROUP BY TRIM,Corte1,UMC1,Descripcion,Id_Contenedor,Ancho,UAncho,TrimCorte,Peso,Rollos
Con esto ya me muestra la sumatoria:
El problema es que ya no me muestra las demas columnas que me mostraba que estan despues del Join:
Código SQL:
Ver original
INNER JOIN (SELECT DISTINCT Descripcion,Id_Contenedor,Ancho,UAncho,TRIM AS TrimCorte,SUM(Peso) Peso ,COUNT(Id_Lote) Rollos
Alguien que me pueda ayudar? :(
De antemano muchas gracias :)