Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在另一个select语句中使用一个select语句中选择的字段?_Sql_Select_Join - Fatal编程技术网

Sql 在另一个select语句中使用一个select语句中选择的字段?

Sql 在另一个select语句中使用一个select语句中选择的字段?,sql,select,join,Sql,Select,Join,我有这个查询,需要向其中添加一个新字段。我可以做一系列的连接来获取字段,但更愿意连接一次,然后在所有其他select语句中使用该字段。以下是查询: select BCCR.DocumentNumber , BCCR.BCCRDebitMemo , BCCR.BCCRDate , BCCR.RejectFlag , BCCR.ManualEntry , BCCR.DebitTotal , coalesce(BCCRApproved.BCCRAmount,0) BCCRA

我有这个查询,需要向其中添加一个新字段。我可以做一系列的连接来获取字段,但更愿意连接一次,然后在所有其他select语句中使用该字段。以下是查询:

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语句中添加一个processeddate字段。我可以在那里加入。我还需要将此字段添加到第三个select语句中。我可以从第二条语句中获取字段并将其添加到第三条语句中,而不必在第三条语句中重复连接吗

在我看来,您似乎想要使用存储过程

然后您可以简单地声明变量并选择结果

一旦分配了变量,就可以在存储过程的范围内随意使用它。

因此,按照第二个链接,选择一个结果作为变量,然后简单地使用该变量。