需要一些SQL存储过程帮助 - 加入

时间:2011-03-02 19:43:05

标签: tsql

所以我有我的proc的这一部分:

SELECT
        com_contact.rc_name_full as CreatedBy,
        capComponent.cm_strike as CapStrike,
        floorComponent.cm_strike as FloorStrike,
        tq_nominal_notional as Notional,
        maxComponent.cm_effective_dt as EffectiveDate,
        maxComponent.cm_maturity_dt as MaturityDate,
        CAST(CAST(DATEDIFF(mm,maxComponent.cm_effective_dt,maxComponent.cm_maturity_dt) as decimal(9,2))/12 as decimal(9,2)) as term,
        (
            CASE WHEN se_amort_term_mnth IS NOT NULL THEN se_amort_term_mnth / 12
            ELSE CAST(CAST(DATEDIFF(mm,
                ISNULL(cmam_amortization_start_dt, maxComponent.cm_effective_dt),
                cmam_amortization_end_dt) as decimal(9,2))/12 as decimal(9,2))
            END
        ) AS AmortTermYears,
        tq_dd_product as Product,
        dh_key_rate as KeyRate,
        dh_pv01 as PV01,
        dh_val_time_stamp as RateTimeStamp,
        re_bnk_le.re_company_name as Company,
        rc_contact_id as UserId,
        stp_name as NickName,
        '' as project,
        '' as Borrower,
        '' as Lender,
        '' as AdditionalInfo,
        CASE WHEN tpm_pd_permission_id = 85 THEN 'LLH' WHEN tpm_pd_permission_id = 86 THEN 'ALM' ELSE '' END as Permission,
        tr_transaction_id as TransactionId,
        NULL as IndicationId

    FROM cfo_transaction

显示'' as project的行,我们现在必须实际更改以返回数据。

名为FROM的{​​{1}}旁边的表格上有一个名为cfo_transaction的ID。我们有另一个名为tr_transaction_id的表,它将这些id与项目ID相关联,使用两个名为:

的列。

com_project_transaction_linkpt_tr_transaction_id,然后我们有一个表格,其中包含名为pt_pj_project_idcom_projectpj_project_id的所有项目。

目标:从该项目表中返回pj_project_name,并将其与正在拉动的交易相关联。

我真的不知道该怎么做。

谢谢!

3 个答案:

答案 0 :(得分:0)

您只需向查询的另一个表添加第二个联接。

select 
    yourfields,
    p.pj_project_name as project
FROM cfo_transaction t
join com_project_transaction_link tl
  on t.tr_transaction_id = tl.pt_tr_transaction_id
join com_project p
  on tl.pt_pj_project_id = p.pj_project_id 

答案 1 :(得分:0)

SELECT ..., cp.pj_project_name
    FROM cfo_transaction ct
        INNER JOIN com_project_transaction_link cptl
            ON ct.tr_transaction_id = cptl.pt_tr_transaction_id
        INNER JOIN com_project cp
            ON cptl.pt_pj_project_id = cp.pj_project_id

答案 2 :(得分:0)

试试这个:

SELECT
        com_contact.rc_name_full as CreatedBy,
        capComponent.cm_strike as CapStrike,
        floorComponent.cm_strike as FloorStrike,
        tq_nominal_notional as Notional,
        maxComponent.cm_effective_dt as EffectiveDate,
        maxComponent.cm_maturity_dt as MaturityDate,
        CAST(CAST(DATEDIFF(mm,maxComponent.cm_effective_dt,maxComponent.cm_maturity_dt) as decimal(9,2))/12 as decimal(9,2)) as term,
        (
            CASE WHEN se_amort_term_mnth IS NOT NULL THEN se_amort_term_mnth / 12
            ELSE CAST(CAST(DATEDIFF(mm,
                ISNULL(cmam_amortization_start_dt, maxComponent.cm_effective_dt),
                cmam_amortization_end_dt) as decimal(9,2))/12 as decimal(9,2))
            END
        ) AS AmortTermYears,
        tq_dd_product as Product,
        dh_key_rate as KeyRate,
        dh_pv01 as PV01,
        dh_val_time_stamp as RateTimeStamp,
        re_bnk_le.re_company_name as Company,
        rc_contact_id as UserId,
        stp_name as NickName,
        PR.pj_project_name as project,
        '' as Borrower,
        '' as Lender,
        '' as AdditionalInfo,
        CASE WHEN tpm_pd_permission_id = 85 THEN 'LLH' WHEN tpm_pd_permission_id = 86 THEN 'ALM' ELSE '' END as Permission,
        tr_transaction_id as TransactionId,
        NULL as IndicationId

    FROM cfo_transaction TR
    INNER JOIN com_project_transaction_link TL
    ON TR.tr_transaction_id = TL.pt_tr_transaction_id
    INNER JOIN com_project PR
    ON TL.pt_pj_project_id = PR.pj_project_id 

上面的查询假设每个事务和项目都在加入项目和事务表的表上(因此INNER JOIN),但是如果你想要,你可以将它们改为LEFT JOIN