我有这个查询,需要向它添加一个新字段。我可以做一堆连接来获取该字段,但宁愿加入一次,然后在所有其他选择语句中使用该字段。以下是查询:
select
BCCR.DocumentNumber
, BCCR.BCCRDebitMemo
, BCCR.BCCRDate
, BCCR.RejectFlag
, BCCR.ManualEntry
, BCCR.DebitTotal
, coalesce(BCCRApproved.BCCRAmount,0) BCCRAmount
, coalesce(BCCRRejected.BCCRRejectedAmount,0) BCCRRejectedAmount
, coalesce(BCCRRejected.BCCRRejectedLines,0) BCCRRejectedLines
from
(select
h.DocumentNumber
, h.DebitMemo as BCCRDebitMemo
, h.TransmissionDate as BCCRDate
, 'N' RejectFlag
, h.ManualEntry
, h.DebitTotal
from chargebackheader h
where h.TransmissionDate >= @BeginDate
and h.TransmissionDate <= @EndDate) BCCR left join
(select
h.DocumentNumber
, ROUND(SUM(d.ChargebackAmount),2) as BCCRAmount
from chargebackheader h join chargebackdetail d on
h.DocumentBranchPlant=d.DocumentBranchPlant
and h.DocumentNumber=d.DocumentNumber
and h.DocumentType=d.DocumentType
where h.TransmissionDate >= @BeginDate
and h.TransmissionDate <= @EndDate
and d.RejectFlag = 'N'
group by
h.DocumentNumber
, h.DebitMemo
, h.TransmissionDate
, h.ManualEntry
, h.DebitTotal) BCCRApproved on BCCR.DocumentNumber = BCCRApproved.DocumentNumber
所以说我想在第二个 BCCR 选择语句中添加一个处理日期字段。我可以在那里加入。我还需要将此字段添加到第三个 select 语句中。我可以从第二个语句中取出该字段并将其添加到第三个语句中,而不必重做第三个语句中的连接吗?