Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Unisciti alla tabella dei clienti e dei tag

usando exists() per ottenere tutti i clienti e i tag quando quel cliente ha un tag di indice 1:

select ct.customernumber, ct.tagindex, c.date
from customers c
  inner join customers_tags ct
    on c.customernumber = ct.customernumber
where exists (
  select 1
  from customers_tags i
  where i.customernumber = ct.customernumber
    and i.tagindex = 1
  )

o usando in() :

select ct.customernumber, ct.tagindex, c.date
from customers c
  inner join customers_tags ct
    on c.customernumber = ct.customernumber
where c.customernumber in  (
  select i.customernumber
  from customers_tags i
  where i.tagindex = 1
  )