Update con cantidad cantidad

08/10/2008 - 00:04 por Aventurero | Informe spam
Tengo una tabla (tabla1) con registros identificación unicos, así

Tabla1
Identificacion
Cantidad (para dejar la cantidad de registros que existen en tabla 2 de esta
identificación)

En otra tabla tengo

Tabla2
Identificacion (varias veces)

Como hago para dejar en Tabla1.Cantidad la cantidad de registros de
tabla2.Identificacion

Gracias, como siempre.

Atentamente,

Aventurero

Preguntas similare

Leer las respuestas

#1 Alejandro Mesa
08/10/2008 - 01:43 | Informe spam
Aventurero,

update t1
set cantidad = (select count(*) from t2 where t2.id = t1.id)
where exists (
select *
from t2
where t2.id = t1.id
)

update a
set a.cantidad = b.cnt
from t1 as a inner join
(select id, count(*) as cnt from t2 group by id) as b
on a.id = b.id

Si quieres tambien actualizar los que no existen en t2, entonces:

update t1
set cantidad = (select count(*) from t2 where t2.id = t1.id)

update a
set a.cantidad = isnull(b.cnt, 0)
from t1 as a left outer join
(select id, count(*) as cnt from t2 group by id) as b
on a.id = b.id


AMB


"Aventurero" wrote:

Mostrar la cita
#2 Aventurero
08/10/2008 - 04:21 | Informe spam
Gracias Alejandro. Que oportuna y buena respuesta.

La segunda sentencia es la que me ha servido de manera excelente.

Mil gracias.

Atentamente,


Aventurero
Ads by Google
Search Busqueda sugerida