This view is working well. Its used in a report building wizard talking to a SQLServer 2008 R2
CREATE view vwQry_LicencePayments
as
select
P.*,
LT.LicenceTypeDesc, FT.FeeTypeDesc, FT.ThroughputText, FT.Prorata as IsProrata, PT.PaymentStatusDesc, PT.IsPaid,
E.EntityType as HolderEntityType, E.EntityTypeDesc as HolderEntityTypeDesc, E.PersonTitle, E.PersonFirstname, E.PersonSurname,
E.OrganisationName, E.FullName as HolderFullName,
E.MailAddress as HolderMailAddress, E.Suburb as HolderSuburb, E.State as HolderState, E.Postcode as HolderPostcode,
E.Mobile as HolderMobile, E.Email, E.PFN as HolderPFN
from
rptPayment P
inner join vwQry_xhsEntity_Base E on P.HolderHistoryID = E.HistoryID
inner join LicenceType LT on P.LicenceTypeID=LT.LicenceTypeID
inner join LicenceFeeType FT on P.FeeTypeID=FT.FeeTypeID
inner join PaymentStatusType PT on P.PaymentStatusID = PT.PaymentStatusID
union ALL
select
P.*,
LT.LicenceTypeDesc, FT.FeeTypeDesc, FT.ThroughputText, FT.Prorata as IsProrata, PT.PaymentStatusDesc, PT.IsPaid,
E.EntityType as HolderEntityType, E.EntityTypeDesc as HolderEntityTypeDesc, E.PersonTitle, E.PersonFirstname, E.PersonSurname,
E.OrganisationName, E.FullName as HolderFullName,
E.MailAddress as HolderMailAddress, E.Suburb as HolderSuburb, E.State as HolderState, E.Postcode as HolderPostcode,
E.Mobile as HolderMobile, E.Email, E.PFN as HolderPFN
from
(select * from rptPayment where HolderHistoryID is null) P
inner join vwQry_Entity E on P.HolderID = E.EntityID
inner join LicenceType LT on P.LicenceTypeID=LT.LicenceTypeID
inner join LicenceFeeType FT on P.FeeTypeID=FT.FeeTypeID
inner join PaymentStatusType PT on P.PaymentStatusID = PT.PaymentStatusID
A requirement to include a payment Date (LP.dateTimeStamp) came up. So adding the extra field (LP.dateTimeStamp) and the two joins (licenceFeePayment tble and licencePayment tble ) necessarily has caused duplicates in the result set. I assume becasue for example if a licence has been paid for in three separate payments it will have three records in the licencefeePayment table. They change I made is:
CREATE view vwQry_LicencePayments
as
select
P.*,
LT.LicenceTypeDesc, FT.FeeTypeDesc, FT.ThroughputText, FT.Prorata as IsProrata, PT.PaymentStatusDesc, PT.IsPaid,
E.EntityType as HolderEntityType, E.EntityTypeDesc as HolderEntityTypeDesc, E.PersonTitle, E.PersonFirstname, E.PersonSurname,
E.OrganisationName, E.FullName as HolderFullName,
E.MailAddress as HolderMailAddress, E.Suburb as HolderSuburb, E.State as HolderState, E.Postcode as HolderPostcode,
E.Mobile as HolderMobile, E.Email, E.PFN as HolderPFN, LP.DatetimeStamp AS DatetimeStamp
from
rptPayment P
inner join vwQry_xhsEntity_Base E on P.HolderHistoryID = E.HistoryID
inner join LicenceType LT on P.LicenceTypeID=LT.LicenceTypeID
inner join LicenceFeeType FT on P.FeeTypeID=FT.FeeTypeID
inner join PaymentStatusType PT on P.PaymentStatusID = PT.PaymentStatusID
inner join licenceFeePayment LFP on P.licenceID = LFP.licenceID
inner join licencePayment LP on LFP.paymentID = LP.paymentID
union ALL
select
P.*,
LT.LicenceTypeDesc, FT.FeeTypeDesc, FT.ThroughputText, FT.Prorata as IsProrata, PT.PaymentStatusDesc, PT.IsPaid,
E.EntityType as HolderEntityType, E.EntityTypeDesc as HolderEntityTypeDesc, E.PersonTitle, E.PersonFirstname, E.PersonSurname,
E.OrganisationName, E.FullName as HolderFullName,
E.MailAddress as HolderMailAddress, E.Suburb as HolderSuburb, E.State as HolderState, E.Postcode as HolderPostcode,
E.Mobile as HolderMobile, E.Email, E.PFN as HolderPFN, LP.DatetimeStamp AS DatetimeStamp
from
(select * from rptPayment where HolderHistoryID is null) P
inner join vwQry_Entity E on P.HolderID = E.EntityID
inner join LicenceType LT on P.LicenceTypeID=LT.LicenceTypeID
inner join LicenceFeeType FT on P.FeeTypeID=FT.FeeTypeID
inner join PaymentStatusType PT on P.PaymentStatusID = PT.PaymentStatusID
inner join licenceFeePayment LFP on P.licenceID = LFP.licenceID
inner join licencePayment LP on LFP.paymentID = LP.paymentID
Help would be very much appreciated. How can I join these two tables and avoid the duplicates?
As @registered user has pointed out, although I have done so, its not as simple as removing ALL from my UNION.
@registered user thank you for your helpful reply. I have made a change to my query based on your help, now have another little issue. I have added a field called 'PaymentTypeDesc' which is from the 'paymentType' table. As this table joins via the 'LicencePayment' I had to place it in your sub query. I am still getting some duplicates because I now believe, not only can multiple payments come in for a licence, they can come from multiple different sources EG EFTPOS, CASH etc....
How can I cater for not only multiple payment transaction but also multiple payment types? Here is my altered working query after using your help:
CREATE view vwQry_LicencePaymentsNew
as
select
P.*,
LT.LicenceTypeDesc, FT.FeeTypeDesc, FT.ThroughputText, FT.Prorata as IsProrata, PT.PaymentStatusDesc, PT.IsPaid,
E.EntityType as HolderEntityType, E.EntityTypeDesc as HolderEntityTypeDesc, E.PersonTitle, E.PersonFirstname, E.PersonSurname,
E.OrganisationName, E.FullName as HolderFullName,
E.MailAddress as HolderMailAddress, E.Suburb as HolderSuburb, E.State as HolderState, E.Postcode as HolderPostcode,
E.Mobile as HolderMobile, E.Email, E.PFN as HolderPFN
, lic.VehicleRegNo as VehicleRegNo
, payment.PaymentTypeDesc as PaymentTypeDesc
, S.StatusTypeDesc as StatusTypeDesc
, Payment.LastPaymentDate as DatetimeStamp
-- , Payment.FirstPaymentDate
from
rptPayment P
inner join vwQry_xhsEntity_Base E on P.HolderHistoryID = E.HistoryID
inner join LicenceType LT on P.LicenceTypeID=LT.LicenceTypeID
inner join LicenceFeeType FT on P.FeeTypeID=FT.FeeTypeID
inner join PaymentStatusType PT on P.PaymentStatusID = PT.PaymentStatusID
inner join licence Lic on P.licenceID = lic.licenceID
inner join vwLicCurrentStatus S on P.LicenceID=S.LicenceID
inner join
(SELECT LFP.licenceID
, FirstPaymentDate = MIN(LP.DatetimeStamp)
, LastPaymentDate = MAX(LP.DatetimeStamp)
, PType.PaymentTypeDesc
FROM licenceFeePayment LFP
INNER JOIN licencePayment LP
on LFP.paymentID = LP.paymentID
INNER JOIN paymentType PType
on LP.paymentTypeID = PType.paymentTypeID
GROUP BY lfp.licenceID, PType.PaymentTypeDesc) Payment
ON P.licenceID = Payment.licenceID
union
select
P.*,
LT.LicenceTypeDesc, FT.FeeTypeDesc, FT.ThroughputText, FT.Prorata as IsProrata, PT.PaymentStatusDesc, PT.IsPaid,
E.EntityType as HolderEntityType, E.EntityTypeDesc as HolderEntityTypeDesc, E.PersonTitle, E.PersonFirstname, E.PersonSurname,
E.OrganisationName, E.FullName as HolderFullName,
E.MailAddress as HolderMailAddress, E.Suburb as HolderSuburb, E.State as HolderState, E.Postcode as HolderPostcode,
E.Mobile as HolderMobile, E.Email, E.PFN as HolderPFN
, lic.VehicleRegNo as VehicleRegNo
, payment.PaymentTypeDesc as PaymentTypeDesc
, S.StatusTypeDesc as StatusTypeDesc
, Payment.LastPaymentDate as DatetimeStamp
-- , Payment.FirstPaymentDate
from rptPayment P
inner join vwQry_xhsEntity_Base E on P.HolderHistoryID = E.HistoryID
inner join LicenceType LT on P.LicenceTypeID=LT.LicenceTypeID
inner join LicenceFeeType FT on P.FeeTypeID=FT.FeeTypeID
inner join PaymentStatusType PT on P.PaymentStatusID = PT.PaymentStatusID
inner join licence Lic on P.licenceID = lic.licenceID
inner join vwLicCurrentStatus S on P.LicenceID=S.LicenceID
inner join
(SELECT LFP.licenceID
, FirstPaymentDate = MIN(LP.DatetimeStamp)
, LastPaymentDate = MAX(LP.DatetimeStamp)
, PType.PaymentTypeDesc
FROM licenceFeePayment LFP
INNER JOIN licencePayment LP
on LFP.paymentID = LP.paymentID
INNER JOIN paymentType PType
on LP.paymentTypeID = PType.paymentTypeID
GROUP BY lfp.licenceID, PType.PaymentTypeDesc) Payment
ON P.licenceID = Payment.licenceID