Hello everyone,
I have some code which I have inherited. It seems to work and looks like it is producing the correct results. For my own understanding I would just like to know how the query is working and how SQL is putting the data together. Here is the code:
(
select distinct c.ref as Client_Id,
c.name,
c.Address,
c.Postcode,
c.Status,
c.type
from customertype ct
inner join customer c on ct.id=c.id
where ct.id IN ('97','98','99')
and c.update_date >='01/01/2020'
intersect
select distinct c.ref as Client_Id,
c.name,
c.Address,
c.Postcode,
c.Status,
c.type
from product p
inner join customer c
on p.ref= c.ref
inner join price pr
on pr.product_code = p.product_code
where p.product_code not in ('AA','BB','BC','BD')
union
(select distinct c.ref as Client_Id,
c.name,
c.Address,
c.Postcode,
c.Status,
c.type
from customertype ct
inner join customer c on ct.id=c.id
where ct.id IN ('97','98', '99')
and c.update_date >='01/01/2020'
intersect
(select distinct c.ref as Client_Id,
c.name,
c.Address,
c.Postcode,
c.Status,
c.type
from product p
inner join client c
on p.ref= c.ref
inner join price pr
on pr.product_code = p.product_code
where (p.product_code not in ('AA','BB','BC','BD') and p.PortfolioID in
(select v.PortfolioID from vw_ProductLookup v
where v.methodid in ('1','2','3'))))
order by c.ref
I haven't used INTERSECT and UNION much so I would be grateful if someone could explain in plain English how it works in this query, and also how this links in with the sub query at the end.
Many thanks for your help.
Vinnie