Ver Mensaje Individual
  #9 (permalink)  
Antiguo 30/10/2013, 15:11
th3r0rn
 
Fecha de Ingreso: noviembre-2007
Mensajes: 504
Antigüedad: 16 años, 6 meses
Puntos: 2
Respuesta: Sumar valores de varias columnas en Join

Cita:
Iniciado por Libras Ver Mensaje
Prueba con esto

Código SQL:
Ver original
  1. SELECT MAX(RowN),C.TRIM,Corte1,UMC1,SUM(Ton1)Ton1 FROM(
  2. SELECT  DISTINCT 1 RowN, TRIM,Corte1,UMC1,Ton1 FROM protri01 WHERE PedPipsa1 = '2073713'
  3. UNION SELECT DISTINCT 2 RowN,TRIM,Corte2,UMC2,Ton2 FROM protri01 WHERE PedPipsa2 ='2073713'
  4. UNION SELECT DISTINCT 3 RowN,TRIM,Corte3,UMC3,Ton3 FROM protri01 WHERE PedPipsa3 = '2073713'
  5. UNION SELECT DISTINCT 4 RowN,TRIM,Corte4,UMC4,Ton4 FROM protri01 WHERE PedPipsa4 ='2073713'
  6. UNION SELECT DISTINCT 5 RowN,TRIM,Corte5,UMC5,Ton5 FROM protri01 WHERE PedPipsa5 = '2073713'
  7. UNION SELECT DISTINCT 6 RowN,TRIM,Corte6,UMC6,Ton6 FROM protri01 WHERE PedPipsa6 ='2073713'
  8. UNION SELECT DISTINCT 7 RowN,TRIM,Corte7,UMC7,Ton7 FROM protri01 WHERE PedPipsa7 = '2073713'
  9. UNION SELECT DISTINCT 8 RowN,TRIM,Corte8,UMC8,Ton8 FROM protri01 WHERE PedPipsa8 ='2073713'
  10. UNION SELECT DISTINCT 9 RowN,TRIM,Corte9,UMC9,Ton9 FROM protri01 WHERE PedPipsa9 = '2073713'
  11. UNION SELECT DISTINCT 10 RowN,TRIM,Corte10,UMC10,Ton10 FROM protri01 WHERE PedPipsa10 ='2073713'
  12. UNION SELECT DISTINCT 11 RowN,TRIM,Corte11,UMC11,Ton11  FROM protri01 WHERE PedPipsa11 = '2073713'
  13. UNION SELECT DISTINCT 12 RowN,TRIM,Corte12,UMC12,Ton12 FROM protri01 WHERE PedPipsa12 ='2073713' ) C
  14. UNION SELECT DISTINCT Descripcion,Id_Contenedor,Ancho,UAncho,TRIM AS TrimCorte,SUM(Peso) Peso ,COUNT(Id_Lote) Rollos
  15. FROM    prolot01  A INNER JOIN  catpap01 B ON B.Id_Papel = A.Id_Papel
  16. WHERE   PedidoPIPSA= '2073713'AND Peso > 0 AND DevProd = 0 AND Jagenberg = 0 AND Detenido = 0 AND Rechazado = 0
  17. GROUP BY Id_Contenedor,Ancho,UAncho,A.TRIM,B.Descripcion,C.TRIM




No tengo gtalk asi que seguiremos por el foro :)

Msg 8120, Level 16, State 1, Line 1
Column 'C.Trim' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 4104, Level 16, State 1, Line 17
The multi-part identifier "C.Trim" could not be bound.
edito:
O sera mejor usar algo asi;
Código SQL:
Ver original
  1. UNION SELECT DISTINCT TRIM,Corte1,UMC1,CASE WHEN Corte1=Corte2 AND Corte3=Corte1 THEN Ton1+Ton2+Ton3 ELSE Ton2 END AS Ton2 FROM protri01 WHERE PedPipsa8 ='2070713'
El pex es que como validaria para todos los casos? :(