Código SQL:
Ver originalSELECT TRIM,Corte1,UMC1,SUM(Ton1)Ton1,Descripcion,Id_Contenedor,Ancho,UAncho,TrimCorte,Peso,Rollos,
CASE WHEN corte8=corte9 THEN Ton8+Ton9 END AS toneladas
FROM(
SELECT DISTINCT TRIM,Corte1,UMC1,Ton1 FROM protri01 WHERE PedPipsa1 = '2070713'
UNION SELECT DISTINCT TRIM,Corte2,UMC2,Ton2 FROM protri01 WHERE PedPipsa2 ='2070713'
UNION SELECT DISTINCT TRIM,Corte3,UMC3,Ton3 FROM protri01 WHERE PedPipsa3 = '2070713'
UNION SELECT DISTINCT TRIM,Corte4,UMC4,Ton4 FROM protri01 WHERE PedPipsa4 ='2070713'
UNION SELECT DISTINCT TRIM,Corte5,UMC5,Ton5 FROM protri01 WHERE PedPipsa5 = '2070713'
UNION SELECT DISTINCT TRIM,Corte6,UMC6,Ton6 FROM protri01 WHERE PedPipsa6 ='2070713'
UNION SELECT DISTINCT TRIM,Corte7,UMC7,Ton7 FROM protri01 WHERE PedPipsa7 = '2070713'
UNION SELECT DISTINCT TRIM,Corte8,UMC8,Ton8 FROM protri01 WHERE PedPipsa8 ='2070713'
UNION SELECT DISTINCT TRIM,Corte9,UMC9,Ton9 FROM protri01 WHERE PedPipsa9 = '2070713'
UNION SELECT DISTINCT TRIM,Corte10,UMC10,Ton10 FROM protri01 WHERE PedPipsa10 ='2070713'
UNION SELECT DISTINCT TRIM,Corte11,UMC11,Ton11 FROM protri01 WHERE PedPipsa11 = '2070713'
UNION SELECT DISTINCT TRIM,Corte12,UMC12,Ton12 FROM protri01 WHERE PedPipsa12 ='2070713' ) 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= '2070713'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