这种观点运作良好。它用于与 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
出现了包含付款日期 (LP.dateTimeStamp) 的要求。因此,添加额外字段(LP.dateTimeStamp)和两个连接(licenceFeePayment tble 和 licencePayment tble)必然会导致结果集中出现重复。我假设是因为例如,如果许可证已在三个单独的付款中支付,它将在 licencefeePayment 表中具有三个记录。我所做的更改是:
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
帮助将不胜感激。如何加入这两个表并避免重复?
正如@registered 用户指出的那样,虽然我已经这样做了,但它并不像从我的 UNION 中删除 ALL 那样简单。
@注册用户感谢您的有用回复。根据您的帮助,我对查询进行了更改,现在还有一个小问题。我添加了一个名为“PaymentTypeDesc”的字段,它来自“paymentType”表。由于该表通过“LicencePayment”连接,因此我必须将其放在您的子查询中。我仍然收到一些重复,因为我现在相信,不仅可以为许可证支付多次付款,它们还可以来自多个不同的来源,例如 EFTPOS、CASH 等....
我如何才能同时满足多种支付交易和多种支付类型?这是我在使用您的帮助后更改的工作查询:
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