Well, I didn’t know how to explain this one on the title, so I’ll be clearer here.
I have a couple of tables:
TiposDeCatalogo:
PK IdTipoCatalogo (int)
IdVendedor (int)
Nombre (string)
Descripcion (string)
Estado (string)
FechaAlta (date)
Catalogo:
PK IdCatalogo (int)
IdTipoCatalogo (int)
IdVendedor (int)
Nombre (string)
Descripcion (string)
Estado (string)
FechaAlta (date)
There is a foreign key on Catalogo.IdTipoCatalogo = TiposDeCatalogos.IdTipoCatalogo
I need a query that can show all rows on TiposDeCatalogo that have already been registered on Catalogo
For instance…
TiposDeCatalogos has the following rows
IdTipoCatalogo IdVendedor Nombre Estado FechaAlta
1 1 Catalogos de Ventas Activo 20/08/2012
2 1 Catalogos de CRM Activo 20/08/2012
3 1 Catalogos de Inventario Activo 20/08/2012
4 1 Catalogos de Facturacion Activo 04/09/2012
5 1 Catalogos de Cobranza Activo 04/09/2012
6 1 Catalogos de Admin Activo 04/09/2012
7 1 Catalogos de Admin Activo 04/09/2012
8 1 Catalogos de Finanzas Activo 04/09/2012
9 1 Catalogos de Clientes Activo 04/09/2012
12 1 Catalogos de Administ General Activo 04/09/2012
Then Catalogo has the following rows
IdCatalogo IdTipoCatalogo IdVendedor Nombre Estado FechaAlta
1 1 17 Formas de Pago formas de pago para las ventas Activo 01/09/2012
2 5 1 Tipos de Moneda Activo 05/09/2012
3 2 1 Tipos de Agendamiento Activo 05/09/2012
4 3 1 Tipos de Precios Activo 06/09/2012
5 5 1 TIpos de Moneda Activo 06/09/2012
6 3 1 Tipo de Presentacion Activo 06/09/2012
You might notice that in Catalogo, only the following IdTipoCatalogo have been registered (1, 2, 3, 5)
So what I want, is a query that shows me those TiposDeCatalogos (only once each, so the query should only return 4 rows in this example)….
I hope you can help me, thanks
You can use
EXISTSto accomplish this:Alternately, you could use a
JOINandDISTINCT:However, this will likely be less efficient, because the
DISTINCTwill require the additional operation to exclude the duplicates produced by theJOIN.