修改SQL视图/添加列而不加入

时间:2013-10-15 15:31:34

标签: sql sql-server view

尝试修改现有视图。它目前在我的数据库上返回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的第四个表,它有一个生效日期列和一个与la_tbl_statement表中的费用ID匹配的费用ID。我想要的是现有视图的所有结果,以及带有生效日期的新列。某些费用没有上限,因此在生效日期列中该行应为NULL。我已尝试加入费用ID,但我获得了19行的子集。

3 个答案:

答案 0 :(得分:0)

使用左外连接将为您提供表的所有结果,即使它不在第4个表中

答案 1 :(得分:0)

在第四个表格上使用左外连接尝试此操作。

    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)

答案 2 :(得分: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)