Wednesday, November 27, 2013

One row per record query

You can use EXISTS...Try the below(Not tested):



select DISTINCT
P.ID
,P.dCreated as 'dtCreated'
,YEAR(P.dStart) 'StartYear'
,Month(P.dStart) 'StartMonth'
,day(P.dStart) 'StartDay'
,P.dStart
,P.dEnd
,DATEADD(YY, 1, P.dStart) as 'dtRenew'
,P.PolicyNo
,C.Name
,DC.Email
,P.NetPremium
,cast(P.Excess as decimal(18,2)) as 'Excess'
,cast(P.GlassExcess as decimal(18,2)) as 'GlassExcess'
,A.Address
,A.Town
,A.PostCode
,cast(P.GUID as varchar(50)) as 'PolicyGUID'
,cast(V.GUID as varchar(50)) as 'VehicleGUID'
,V.RegNo
from dbo.Policy P
Inner join dbo.Company C on C.ID = P.HolderID
Inner join dbo.Contact DC on C.PrimaryContactID = DC.ID
inner join dbo.Address A on A.ID = DC.AddressID
inner join dbo.Vehicle V on V.ID = PC.VehicleID
Where exists
(
SELECT 1 FROM dbo.PolicyCover PC2 where PC2.PolicyID=P.ID
)


No comments:

Post a Comment