Combinar 2 tablas
Combinar 2 tablas
Tengo el siguiente problema, como hacer para combinar 2 tablas. Ej. Valores. Tabla#1 (c1 c2 c3 v1 v2 v3 r1 r2 r3) (a a a 0 0 0 1 1 1) (a b b 0 0 0 3 3 3) (a b c 0 0 0 5 5 5) (t 0 0 0 9 9 9). Tabla#2 (c1 c2 c3 v1 v2 v3 r1 r2 r3) (a a a 2 2 2 0 0 0) (a b b 4 4 4 0 0 0) (a b c 6 6 6 0 0 0) (t 12 12 12 0 0 0). Como hacer para combinarlas y queden así: Tabla#3 Final (c1 c2 c3 v1 v2 v3 r1 r2 r3) (a a a 1 1 1 2 2 2) (a b b 3 3 3 4 4 4) (a b c 5 5 5 6 6 6) (t 9 9 9 12 12 12) Nota: las filas y columnas de las tablas van ser siempre las mismas y con la misma cantidad de registros para los campos c1 c2 c3 Como hacer para que la tabla#1 y #2 se combinen. El codigo de consulta SQL para las tablas es: Tabla#1 { SELECT tabla_c.c1 AS c1, tabla_c.c2 AS c2, tabla_c.c3 AS c3, SUM(tabla_v.v1) AS v1, SUM(tabla_v.v2) AS v2, SUM(tabla_v.v3) AS v3, 0 AS r1, 0 AS r2, 0 AS r3 FROM tabla_c, tabla_v WHERE (tabla_c.codigo = tabla_v.codigo) GROUP BY tabla_c.c1, tabla_c.c2, tabla_c.c3 UNION ALL SELECT tabla_c.c1 AS c1, tabla_c.c2 AS c2, tabla_c.c3 AS c3, SUM(tabla_v.v1) AS v1, SUM(tabla_v.v2) AS v2, SUM(tabla_v.v3) AS v3, 0 AS r1, 0 AS r2, 0 AS r3 FROM tabla_c, tabla_v WHERE (tabla_c.codigo = tabla_v.codigo) ORDER BY tabla_c.c1, tabla_c.c2, tabla_c.c3 } Tabla#2 { SELECT tabla_c.c1 AS c1, tabla_c.c2 AS c2, tabla_c.c3 AS c3, 0 AS v1, 0 AS v2, 0 AS v3, SUM(tabla_r.r1) AS r1, SUM(tabla_r.r2) AS r2, SUM(tabla_r.r3) AS r3 FROM tabla_c, tabla_r WHERE (tabla_c.codigo = tabla_r.codigo) GROUP BY tabla_c.c1, tabla_c.c2, tabla_c.c3 UNION ALL SELECT tabla_c.c1 AS c1, tabla_c.c2 AS c2, tabla_c.c3 AS c3, 0 AS v1, 0 AS v2, 0 AS v3, SUM(tabla_r.r1) AS r1, SUM(tabla_r.r2) AS r2, SUM(tabla_r.r3) AS r3 FROM tabla_c, tabla_r WHERE (tabla_c.codigo = tabla_r.codigo) ORDER BY tabla_c.c1, tabla_c.c2, tabla_c.c3 } como hacer para que muestre los valores como en la tabla#3 final. Pueden darme sus sugerencias. (tengo problemas para darle un formato comprensible así en el foro, si puedes pegala en un editor para un mejor visialuzación de lo que pido)
Tengo el siguiente problema, como hacer para combinar 2 tablas. Ej. Valores. Tabla#1 (c1 c2 c3 v1 v2 v3 r1 r2 r3) (a a a 0 0 0 1 1 1) (a b b 0 0 0 3 3 3) (a b c 0 0 0 5 5 5) (t 0 0 0 9 9 9). Tabla#2 (c1 c2 c3 v1 v2 v3 r1 r2 r3) (a a a 2 2 2 0 0 0) (a b b 4 4 4 0 0 0) (a b c 6 6 6 0 0 0) (t 12 12 12 0 0 0). Como hacer para combinarlas y queden así: Tabla#3 Final (c1 c2 c3 v1 v2 v3 r1 r2 r3) (a a a 1 1 1 2 2 2) (a b b 3 3 3 4 4 4) (a b c 5 5 5 6 6 6) (t 9 9 9 12 12 12) Nota: las filas y columnas de las tablas van ser siempre las mismas y con la misma cantidad de registros para los campos c1 c2 c3 Como hacer para que la tabla#1 y #2 se combinen. El codigo de consulta SQL para las tablas es: Tabla#1 { SELECT tabla_c.c1 AS c1, tabla_c.c2 AS c2, tabla_c.c3 AS c3, SUM(tabla_v.v1) AS v1, SUM(tabla_v.v2) AS v2, SUM(tabla_v.v3) AS v3, 0 AS r1, 0 AS r2, 0 AS r3 FROM tabla_c, tabla_v WHERE (tabla_c.codigo = tabla_v.codigo) GROUP BY tabla_c.c1, tabla_c.c2, tabla_c.c3 UNION ALL SELECT tabla_c.c1 AS c1, tabla_c.c2 AS c2, tabla_c.c3 AS c3, SUM(tabla_v.v1) AS v1, SUM(tabla_v.v2) AS v2, SUM(tabla_v.v3) AS v3, 0 AS r1, 0 AS r2, 0 AS r3 FROM tabla_c, tabla_v WHERE (tabla_c.codigo = tabla_v.codigo) ORDER BY tabla_c.c1, tabla_c.c2, tabla_c.c3 } Tabla#2 { SELECT tabla_c.c1 AS c1, tabla_c.c2 AS c2, tabla_c.c3 AS c3, 0 AS v1, 0 AS v2, 0 AS v3, SUM(tabla_r.r1) AS r1, SUM(tabla_r.r2) AS r2, SUM(tabla_r.r3) AS r3 FROM tabla_c, tabla_r WHERE (tabla_c.codigo = tabla_r.codigo) GROUP BY tabla_c.c1, tabla_c.c2, tabla_c.c3 UNION ALL SELECT tabla_c.c1 AS c1, tabla_c.c2 AS c2, tabla_c.c3 AS c3, 0 AS v1, 0 AS v2, 0 AS v3, SUM(tabla_r.r1) AS r1, SUM(tabla_r.r2) AS r2, SUM(tabla_r.r3) AS r3 FROM tabla_c, tabla_r WHERE (tabla_c.codigo = tabla_r.codigo) ORDER BY tabla_c.c1, tabla_c.c2, tabla_c.c3 } como hacer para que muestre los valores como en la tabla#3 final. Pueden darme sus sugerencias. (tengo problemas para darle un formato comprensible así en el foro, si puedes pegala en un editor para un mejor visialuzación de lo que pido)
1 Respuesta
Respuesta de yodelmis
1