SQL如何交叉引用两个不相关的表

时间:2015-10-26 15:56:56

标签: sql sql-server sql-server-2012

如果来自另一个表的contindex匹配,我需要从这两个表中交叉引用并仅显示来自第一个表的匹配的contindex。下面是我所拥有的图像。代码也粘贴在下面。

select t.ContIndex, t.clientcode, t.debttranname as ClientName, DebtDetService,
case when DebtTranType = 3 then 'Invoice' else 'Credit Memo' end as Type, d.amount,
REPLACE(REPLACE(REPLACE(CAST(FeeNarrative As varchar(max)),
CHAR(10) + CHAR(13), ' '), CHAR(10), ' '), CHAR(13), ' ') as Narrative
from tblTranDebtorDetail d
inner join tbltrandebtor t on t.DebtTranIndex=d.DebtTranIndex
where DebtTranDate > 'jan 1 2015' and t.debttrantype in (3,6) 
and DebtDetService = 'abr reimb'

select w.contindex /*, ClientCode, ClientName, Job_Name, 
case when TransTypeIndex=1 then 'Time' else 'Exp' end as Type, sum(wipamount)*/
from tblTranWIP w
inner join tblJob_Header h on h.job_idx=w.ServPeriod and h.ContIndex=w.ContIndex
inner join tblengagement e on e.ContIndex=w.ContIndex
inner join tblstaff s on s.StaffIndex=w.StaffIndex
where wipoutstanding <>0 and h.Job_Template in (99,100) and TransTypeIndex in (1,2)
--group by w.contindex, ClientCode, ClientName, Job_Name, TransTypeIndex

Preview of what I have

基本上我只需要显示与底部表contindex匹配的顶部表行。

2 个答案:

答案 0 :(得分:3)

听起来这些表与contindex相关,不是吗?无论如何,复制和粘贴你的代码并添加一些额外的东西应该可以做到这一点:

select t.ContIndex, t.clientcode, t.debttranname as ClientName, DebtDetService,
case when DebtTranType = 3 then 'Invoice' else 'Credit Memo' end as Type, d.amount,
REPLACE(REPLACE(REPLACE(CAST(FeeNarrative As varchar(max)),
CHAR(10) + CHAR(13), ' '), CHAR(10), ' '), CHAR(13), ' ') as Narrative
from tblTranDebtorDetail d
inner join tbltrandebtor t on t.DebtTranIndex=d.DebtTranIndex
where DebtTranDate > 'jan 1 2015' and t.debttrantype in (3,6) 
and DebtDetService = 'abr reimb'
and t.ContIndex IN
(

    select w.contindex /*, ClientCode, ClientName, Job_Name, 
    case when TransTypeIndex=1 then 'Time' else 'Exp' end as Type, sum(wipamount)*/
    from tblTranWIP w
    inner join tblJob_Header h on h.job_idx=w.ServPeriod and h.ContIndex=w.ContIndex
    inner join tblengagement e on e.ContIndex=w.ContIndex
    inner join tblstaff s on s.StaffIndex=w.StaffIndex
    where wipoutstanding <>0 and h.Job_Template in (99,100) and TransTypeIndex in (1,2)
    --group by w.contindex, ClientCode, ClientName, Job_Name, TransTypeIndex
)

附加代码是WHERE t.contindex IN (<yoursecondquery>)你也可以用INNER JOIN完成同样的事情,但这是一个快速修复,因为你已经分别写出了两个查询。

答案 1 :(得分:2)

您可以在此处使用IN或EXISTS ..如果您使用EXISTS,则只需稍微更改第二个查询以添加WHERE条件。

SELECT
    t.ContIndex,
    t.clientcode,
    t.debttranname AS ClientName,
    DebtDetService,
    CASE WHEN DebtTranType = 3 THEN 'Invoice'
         ELSE 'Credit Memo'
    END AS Type,
    d.amount,
    REPLACE(REPLACE(REPLACE(CAST(FeeNarrative AS VARCHAR(MAX)),CHAR(10) + CHAR(13),' '),CHAR(10),' '),CHAR(13),' ') AS Narrative
FROM
    tblTranDebtorDetail d
    INNER JOIN tbltrandebtor t ON t.DebtTranIndex = d.DebtTranIndex
WHERE
    DebtTranDate > 'jan 1 2015'
    AND t.debttrantype IN (3,6)
    AND DebtDetService = 'abr reimb'
    AND EXISTS ( SELECT
                    w.contindex
                 FROM
                    tblTranWIP w
                    INNER JOIN tblJob_Header h ON h.job_idx = w.ServPeriod
                                                  AND h.ContIndex = w.ContIndex
                    INNER JOIN tblengagement e ON e.ContIndex = w.ContIndex
                    INNER JOIN tblstaff s ON s.StaffIndex = w.StaffIndex
                 WHERE
                    t.ContIndex = w.contindex -- new where clause
                    AND wipoutstanding <> 0
                    AND h.Job_Template IN (99,100)
                    AND TransTypeIndex IN (1,2) )