0

既存のビューを変更しようとしています。現在、DB で 32 行を返します。コードは次のとおりです。

ALTER VIEW [dbo].[vw_fnd_la_Statement]
AS

SELECT     
    SH.lease_id, S.expense_id, SH.statement_header_id, S.statement_id AS row_id,
    EC.name AS expense_category, SH.period, S.invoice_number, 
    S.received_date, S.due_date, S.landlord_expense_total, S.prorata_share,   
    S.tenant_expense_total, S.tenant_admin_fee, S.paid_estimates, S.net_due, 
    CASE 
        WHEN s.status = 'O' THEN 'Open' 
        WHEN s.status = 'P' THEN 'In Progress' 
        WHEN s.status = 'D' THEN 'In Dispute' 
        WHEN s.status = 'C' THEN 'Closed' 
        ELSE NULL 
    END AS status, S.approval, 
    dbo.UDF_getExpenseVendor(S.expense_id) AS vendor, S.approved_amount, 
    S.approved_payment_amount, S.disputed_amount, S.held_amount, S.statement_id, 
    EC.expense_category_id, SH.start_date, SH.end_date, S.status AS status_id
FROM      
    la_tbl_statement_header AS SH 
INNER JOIN
    la_tbl_statement AS S ON SH.statement_header_id = S.statement_header_id 
INNER JOIN
    la_tlu_expense_category AS EC ON S.expense_category_id = EC.expense_category_id
WHERE     
    (SH.IsDeleted = 0) AND (S.IsDeleted = 0) AND (EC.IsDeleted = 0)

la_tbl_expense_cap_setup有効日列と、テーブル内の経費 ID と一致する経費 ID を持つ4 番目のテーブルがありla_tbl_statementます。私が欲しいのは、既存のビューからのすべての結果と、有効日を含む新しい列です。一部の経費には上限がないため、その行は発効日列で NULL にする必要があります。経費 ID を結合しようとしましたが、19 行のサブセットが得られます。

4

3 に答える 3

0

4 番目のテーブルの左外部結合でこれを試してください。

    ALTER VIEW [dbo].[vw_fnd_la_Statement]
    AS

    SELECT     SH.lease_id, S.expense_id, SH.statement_header_id, S.statement_id AS row_id,          EC.name AS expense_category, SH.period, S.invoice_number, 
          S.received_date, S.due_date, S.landlord_expense_total, S.prorata_share,   S.tenant_expense_total, S.tenant_admin_fee, S.paid_estimates, S.net_due, 
          CASE WHEN s.status = 'O' THEN 'Open' WHEN s.status = 'P' THEN 'In Progress' WHEN s.status = 'D' THEN 'In Dispute' WHEN s.status = 'C' THEN 'Closed' ELSE
           NULL END AS status, S.approval, dbo.UDF_getExpenseVendor(S.expense_id) AS vendor, S.approved_amount, S.approved_payment_amount, 
          S.disputed_amount, S.held_amount, S.statement_id, EC.expense_category_id, SH.start_date, SH.end_date, S.status AS status_id, esc.effective_date
    FROM      
            la_tbl_statement_header AS SH INNER JOIN
          la_tbl_statement AS S ON SH.statement_header_id = S.statement_header_id INNER   JOIN
          la_tlu_expense_category AS EC ON S.expense_category_id = EC.expense_category_id
          left outer join la_tbl_expense_cap_setup as ecson esc.expense_id   = s.expense_id
    WHERE     (SH.IsDeleted = 0) AND (S.IsDeleted = 0) AND (EC.IsDeleted = 0)
于 2013-10-15T15:37:41.487 に答える
0

OK LEFT OUTER JOIN はおおむね正しかった。正しく機能させるには、新しい結合に「AND (ECS.IsDeleted = 0)」を追加する必要がありました。私は元々、有効な日付を持つ行のみを返す WHERE 句にこの AND を持っていました。これが正しいコードです。助けてくれてありがとう。

SELECT     SH.lease_id, S.expense_id, SH.statement_header_id, S.statement_id AS row_id, EC.name AS expense_category, SH.period, S.invoice_number, 
      S.received_date, S.due_date, S.landlord_expense_total, S.prorata_share, S.tenant_expense_total, S.tenant_admin_fee, S.paid_estimates, S.net_due, 
      CASE WHEN s.status = 'O' THEN 'Open' WHEN s.status = 'P' THEN 'In Progress' WHEN s.status = 'D' THEN 'In Dispute' WHEN s.status = 'C' THEN 'Closed' ELSE
       NULL END AS status, S.approval, dbo.UDF_getExpenseVendor(S.expense_id) AS vendor, S.approved_amount, S.approved_payment_amount, 
      S.disputed_amount, S.held_amount, S.statement_id, EC.expense_category_id, SH.start_date, SH.end_date, S.status AS status_id,
      ECS.effective_date AS cap_eff_date
FROM      
        la_tbl_statement_header AS SH INNER JOIN
      la_tbl_statement AS S ON SH.statement_header_id = S.statement_header_id INNER JOIN
      la_tlu_expense_category AS EC ON S.expense_category_id = EC.expense_category_id left outer join
      la_tbl_expense_cap_setup AS ECS ON s.expense_id = ECS.expense_id AND (ECS.IsDeleted = 0)
WHERE     (SH.IsDeleted = 0) AND (S.IsDeleted = 0) AND (EC.IsDeleted = 0)
于 2013-10-15T22:37:01.060 に答える