2015-10-26 70 views
-1

如果來自另一個表的contindex匹配,我需要從這兩個表中交叉引用並僅顯示第一個表中的匹配contindex。下面是我的圖片。代碼也被粘貼在下面。SQL如何交叉引用兩個不相關的表

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的頂級錶行。

+0

你可以改變任一查詢嗎? – JamieD77

回答

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過,但是這是一個單觸因爲你已經寫出了兩個查詢分開。

+0

是的,我不是說'不'。刪除!謝謝 – JNevill

+0

謝謝你的解釋!有用! – andres

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)) 
+0

這種方法也適用!謝謝! – andres