Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

One row per record query

$
0
0

Hi all

I have a query which was returning duplicate rows.  This is because one of the tables it's referring to has duplicates in it.  I would like to limit the query to only return one row per record.  I have limited the rows returned using this:

select 
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

Are there any alternatives to what I have implemented?  Anything that can be implemented in the WHERE clause perhaps?

Thanks in advance.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>