SQL Server - 使用子查询中主查询的列

Joã*_*rme 37 sql sql-server field subquery

有没有办法从主查询中实时获取列,并在子查询中使用它?

这样的事情:(在子查询中使用A.item)

SELECT item1, * 
FROM TableA A 
INNER JOIN 
(
    select * 
    from TableB B 
    where A.item = B.item
) on A.x = B.x;
Run Code Online (Sandbox Code Playgroud)

好的,这是真的:

我需要修改这个现有的查询.它以前工作过,但现在数据库发生了变化,我需要做一些修改,添加一些比较.正如您所看到的,有很多JOINS,其中一个是子查询.我需要从一列从主查询(例如从表T0)增加相比,子查询(是这样的:T6.UnionAll_Empresa = T0.UnionALl_Empresa)

Select T0.UnionAll_Empresa,<STUFF>

from [UNION_ALL_BASES]..OINV T0 with (nolock)
inner join [UNION_ALL_BASES]..INV6 T1 with (nolock) on t0.DocEntry = t1.DocEntry and t0.UnionAll_Empresa = t1.UnionAll_Empresa
inner join

(
select 
t1.CompanyID,
T2.CompanyDb,
t1.OurNumber,
T6.BankCode,
T6.BankName,
T3.[Description] Situation,
T1.[Status],
T5.Descrption nomeStatus,
T1.Origin,
T1.DocEntry,
T1.DocType,
T1.ControlKey,
T1.CardCode,
T4.[Description] ContractBank,
T1.PayMethodCode,
T1.DueDate,
T1.DocDate,
T1.InstallmentID,
T1.InstallmentValue,
T1.Correction,
T1.InterestContractural,
T1.FineContract,
T1.ValueAbatment,
T1.ValueDiscount,
T1.ValueFineLate,
T1.ValueInterestDaysOfLate,
T1.OtherIncreases,
T1.ValueInWords,
T1.ValueDocument,
T1.DigitalLine,
T1.Document
from [IntegrationBank]..BillOfExchange T1 with (nolock)
    inner join [InterCompany2]..CompanyHierarchy T2 with (nolock) on T1.CompanyID = T2.ID
    left join [IntegrationBank]..BillOfExchangeSituation T3 with (nolock) on T1.Situation = T3.ID 
    inner join [IntegrationBank]..ContractBank T4 with (nolock) on T1.ContractBank = T4.ID 
    inner join [IntegrationBank]..BoeStatus T5 with (nolock) on T1.[Status] = T5.ID 
    inner join [UNION_ALL_BASES]..ODSC T6 with (nolock) on T4.BankKey = T6.AbsEntry and **T6.UnionAll_Empresa = T0.UnionALl_Empresa** --I need to do this 
where T1.[Status] <> 5 
and T2.CompanyDb = **T0.UnionAll_Empresa** --I need to do this
) TBI on (T1.DocEntry = TBI.DocEntry and T1.InstlmntID = TBI.InstallmentID and TBI.DocType = T1.ObjType )
inner join [UNION_ALL_BASES]..OCTG T2 on T0.GroupNum = T2.GroupNum and T0.UnionAll_Empresa = T2.UnionAll_Empresa
inner join [UNION_ALL_BASES]..OSLP T3 on T0.SlpCode = T3.SlpCode and T0.UnionAll_Empresa = T3.UnionAll_Empresa
where not exists (select 1
        from [UNION_ALL_BASES]..RIN1 A with (nolock) 
                inner join [UNION_ALL_BASES]..ORIN B with (nolock) on A.DocEntry = B.DocEntry and A.UnionAll_Empresa = B.UnionAll_Empresa
        where A.BaseEntry = T0.DocEntry
        and   B.SeqCode = ''1'' )
Run Code Online (Sandbox Code Playgroud)

mmm*_*mmm 39

您可以使用OUTER APPLY

   SELECT  *
    FROM    tbl1
            OUTER APPLY ( SELECT TOP 1
                                    currency_id,
                                    SUM(taxrate) AS taxrate
                          FROM      tbl2
                          WHERE     wuptr.currency_id = tbl1.currency_id
                          GROUP BY  tbl2.currencyid
                        ) 
Run Code Online (Sandbox Code Playgroud)


JNK*_*JNK 19

您不需要子查询:

SELECT item1, * 
FROM TableA A 
INNER JOIN 
   TableB B 
     ON A.item = B.item
     AND A.x = B.x;
Run Code Online (Sandbox Code Playgroud)

我想不出你需要JOIN在带有过滤器的子查询上的场景,它不等同于直接在外部查询中引用该字段.

但是,您可以在WHERE子句中的子查询中引用外部表:

SELECT <stuff>
FROM Table t
WHERE EXISTS  (SELECT 1 from TableB B 
               WHERE t.id = b.id)
Run Code Online (Sandbox Code Playgroud)

编辑

对于您的实际代码,只需将JOIN条件更改为:

) TBI on (T1.DocEntry = TBI.DocEntry
          and T1.InstlmntID = TBI.InstallmentID 
          and TBI.DocType = T1.ObjType
          AND TBI.CompanyDB = T0.UnionAll_Empresa )
Run Code Online (Sandbox Code Playgroud)


小智 12

如果你想加入子查询并"实时获取一个列"/引用主查询中的一列,那么就有一个技巧可以做到这一点.

如果将子查询用作别名表,则无法访问子查询之外的表,换句话说,此SQL永远不能访问A:

...
INNER JOIN 
(
    select * 
    from TableB B 
    where A.item = B.item
) on A.x = B.x;
Run Code Online (Sandbox Code Playgroud)

访问A的方式如下:

SELECT item1, * 
FROM TableA A 
INNER JOIN TableB on TableB.item = TableA.item and TableB.item in
(
    select top 1 B.Item
    from TableB B 
    where A.item = B.item
)
Run Code Online (Sandbox Code Playgroud)

只要忽略"前1"部分,我只是添加了这一点,以表明可能有理由进行这样的连接.
因此,基本上如果要在子查询中引用查询中的项,只需将子查询移动到连接的ON部分,并使用IN关键字,如上所示.


Moh*_*ieb 9

您可以通过命名主查询和嵌套查询的表来完成此操作.例如:

SELECT continent, name, population FROM world x
  WHERE population >= ALL
    (SELECT population FROM world y
        WHERE y.continent=x.continent
          AND population>0)
Run Code Online (Sandbox Code Playgroud)

参考:http://sqlzoo.net/wiki/SELECT_within_SELECT_Tutorial