Unir dos tablas en PostgreSQL
Frecuentes
Visto 306 veces
-2
Quiero combinar estas dos tablas.
Quiero insertar las nuevas filas y actualizar los valores en el recuento de columnas si coinciden con los valores en las filas
La tabla 1 es un objetivo, la tabla 2 es la fuente.
Tabla 1
c1 c2 c3 c4 c5 number
1 2 3 4 5 3
2 3 4 5 6 2
2 3 5 6 7 2
Tabla 2
c1 c2 c3 c4 c5 number
1 3 4 5 6 3
1 2 3 4 5 2
¿Puede una consulta de selección devolver el orden de los datos en el siguiente formato?
Resultado (Tabla 1)
c1 c2 c3 c4 c5 number
1 2 3 4 5 5
1 3 4 5 6 3
2 3 4 5 6 2
2 3 5 6 7 2
3 Respuestas
1
si no le importa eliminar datos de Table1 y luego insertar nuevos datos, puede hacer esto:
with cte1 as (
delete from Table1
returning *
), cte2 as (
select c1, c2, c3, c4, c5, cnt from cte1
union all
select c1, c2, c3, c4, c5, cnt from Table2
)
insert into Table1
select c1, c2, c3, c4, c5, sum(cnt)
from cte2
group by c1, c2, c3, c4, c5;
si realmente desea actualizar/insertar datos, puede hacer esto:
with cte_upsert as (
update Table1 as T1 set
cnt = T1.cnt + T2.cnt
from Table2 as T2
where
T1.c1 = T2.c1 and T1.c2 = T2.c2 and
T1.c3 = T2.c3 and T1.c4 = T2.c4 and
T1.c5 = T2.c5
returning T1.*
)
insert into Table1
select T2.c1, T2.c2, T2.c3, T2.c4, T2.c5, T2.cnt
from Table2 as T2
where
not exists (
select *
from cte_upsert as T1
where
T1.c1 = T2.c1 and T1.c2 = T2.c2 and
T1.c3 = T2.c3 and T1.c4 = T2.c4 and
T1.c5 = T2.c5
);
o puedes hacer la más obvia:
update Table1 as T1 set
cnt = T1.cnt + T2.cnt
from Table2 as T2
where
T1.c1 = T2.c1 and T1.c2 = T2.c2 and
T1.c3 = T2.c3 and T1.c4 = T2.c4 and
T1.c5 = T2.c5;
insert into Table1
select T2.c1, T2.c2, T2.c3, T2.c4, T2.c5, T2.cnt
from Table2 as T2
where
not exists (
select *
from Table1 as T1
where
T1.c1 = T2.c1 and T1.c2 = T2.c2 and
T1.c3 = T2.c3 and T1.c4 = T2.c4 and
T1.c5 = T2.c5
);
Respondido el 21 de Septiembre de 13 a las 13:09
0
Sé que no es una solución optimizada, pero resolverá su problema
select
a.c1 as c1 , a.c2 as c2, a.c3 as c3, a.c4 as c4, a.c5 as c5, a.count + b.count as count
from
t1 a
join
t2 b ON (a.c1 = b.c1 and a.c2 = b.c2
and a.c3 = b.c3
and a.c4 = b.c4
and a.c5 = b.c5)
union all
(select
a.c1 as c1 , a.c2 as c2, a.c3 as c3, a.c4 as c4, a.c5 as c5, a.count as count
from
t1 a
LEFT join
t2 b ON (a.c1 = b.c1 and a.c2 = b.c2
and a.c3 = b.c3
and a.c4 = b.c4
and a.c5 = b.c5)
WHERE b.c1 is null)
UNION all
select
a.c1 as c1 , a.c2 as c2, a.c3 as c3, a.c4 as c4, a.c5 as c5, a.count as count
from
t2 a
LEFT join
t1 b ON (a.c1 = b.c1 and a.c2 = b.c2
and a.c3 = b.c3
and a.c4 = b.c4
and a.c5 = b.c5)
WHERE b.c1 is null
Respondido el 21 de Septiembre de 13 a las 13:09
0
No está claro si el "conteo" es la función agregada de una consulta anterior o un campo simple, sin embargo, si es un campo simple, como en los siguientes datos:
CREATE TABLE table1(
c1 integer,
c2 integer,
c3 integer,
c4 integer,
c5 integer,
count integer
);
INSERT INTO table1 VALUES (1,2,3,4,5,3);
INSERT INTO table1 VALUES (2,3,4,5,6,2);
INSERT INTO table1 VALUES (2,3,5,6,7,2);
CREATE TABLE table2(
c1 integer,
c2 integer,
c3 integer,
c4 integer,
c5 integer,
count integer
);
INSERT INTO table2 VALUES (1,3,4,5,6,3);
INSERT INTO table2 VALUES (1,2,3,4,5,2);
Puedes obtener tus datos de esta forma:
SELECT c1, c1,c2, c3, c4,c5,SUM(count) AS count
FROM (
SELECT * FROM table1
UNION ALL
SELECT * FROM table2) AS foo
GROUP BY c1, c2, c3, c4, c5
ORDER BY c1, c2, c3, c4, c5
espero que esto pueda ayudarte
Respondido el 21 de Septiembre de 13 a las 18:09
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas sql postgresql join or haz tu propia pregunta.
"Las preguntas que solicitan código deben demostrar una comprensión mínima del problema que se está resolviendo. Incluya los intentos de solución, por qué no funcionaron y los resultados esperados" - zero323
en sql,
count()
es el nombre de una función agregada. Mejor no usarlo como nombre de columna - wildplasser