Wednesday, November 27, 2013

One row per record query

Try the below:DISTINCT



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 PolicyCover PC on PC.PolicyID = P.ID
inner join dbo.Address A on A.ID = DC.AddressID
inner join dbo.Vehicle V on V.ID = PC.VehicleID
inner join
(
SELECT MAX(ID) AS MID, PolicyID FROM dbo.PolicyCover GROUP BY PolicyID
) PC2 ON PC.ID = PC2.MID


No comments:

Post a Comment