需要将计算列添加到复杂的SQL Server视图

时间:2013-06-07 16:00:41

标签: sql sql-server view

我是SQL Server的新手,我正在处理一个视图,该视图是从2个select语句的联合创建的,这些语句在视图中创建列并使用内部联接,左外部联接等。见下面的脚本。

我添加了

(SUM(PR.amount) OVER (PARTITION BY YEAR(PR.posting_date), PR.lease_id, E.expense_id)) as Annual

创建一个列,显示每月个人费用的年度总和。不幸的是,因为SUM行在联合的两个部分中,我没有得到期望的结果。当posting_datelease_idexpense id相同时,年度列中的总和应该相同。但是,即使所有这些参数都匹配,联盟的一半中的项目与联盟另一半的项目的总和也不同。

基本上我需要做的(我认为)是在union完成之后创建年度列,以便sum函数在进行计算时看到union的两个部分中的所有项。我正在考虑将整个事物嵌套在SELECT并将SUM线移出联盟的两个部分,但我不知道这是否有效或是否正确。

提前感谢您的建议。

create view xyz
as
SELECT     PR.amount_currency_code, CASE WHEN P.lease_payment_id IS NULL THEN PR.amount ELSE P.amount END AS amount, 
                      CASE WHEN P.lease_payment_id IS NULL THEN PR.gl_code ELSE P.gl_code END AS gl_code, 
                      CASE WHEN PR.one_time_expense_flag = 0 THEN 'N' ELSE 'Y' END AS One_Time_Flag, 
                      dbo.UDF_getRentSummaryVendor(E.expense_id, P.lease_payment_id, PR.lease_projection_id) AS vendor,
                      dbo.UDF_getRentSummaryVendorNo(E.expense_id, P.lease_payment_id, PR.lease_projection_id) as vendor_no, 
                      PS.name AS Payment_Status, PR.lease_id, PR.row_id, EC.name AS Expense_Category, P.payment_status_id, 
                      PR.lease_projection_id, E.expense_status_id, PR.posting_date,
                      PR.scheduled_payment_date AS payment_date, PR.expense_category_id,
              3 AS alloc_type,
              'Non-Standard' AS alloc_type_desc,
              'Check Details' as checkdetail,

              (SUM(PR.amount) OVER (PARTITION BY YEAR(PR.posting_date), PR.lease_id, E.expense_id)) as Annual

FROM         dbo.la_tbl_lease_projection AS PR INNER JOIN   
                      dbo.la_tbl_lease AS L ON PR.lease_id = L.lease_id INNER JOIN
                      dbo.la_tlu_lease_status AS LS ON L.status_id = LS.status_id INNER JOIN                      
                      dbo.la_tlu_held_by AS HB ON L.heldby_id = HB.heldby_id INNER JOIN                                   
                      la_tbl_lease_proj_nonstd_alloc AS PA on PA.lease_projection_id = PR.lease_projection_id and PA.menu_id_key = PA.menu_id_key and PA.percentage > 0 LEFT OUTER JOIN
                      dbo.la_tbl_expense AS E ON E.expense_id = PR.expense_id LEFT OUTER JOIN
                      dbo.la_tbl_lease_payment AS P ON PR.lease_projection_id = P.lease_projection_id LEFT OUTER JOIN
                      dbo.la_tlu_payment_status AS PS ON P.payment_status_id = PS.payment_status_id LEFT OUTER JOIN
                      dbo.la_tlu_expense_category AS EC ON PR.expense_category_id = EC.expense_category_id
WHERE     (PR.IsDeleted = 0) AND (E.IsDeleted = 0 OR
                      E.IsDeleted IS NULL)
                      and HB.system_type <> '2' and HB.IsDeleted = 0

union

SELECT     PR.amount_currency_code, CASE WHEN P.lease_payment_id IS NULL THEN PR.amount ELSE P.amount END AS amount, 
                      CASE WHEN P.lease_payment_id IS NULL THEN PR.gl_code ELSE P.gl_code END AS gl_code, 
                      CASE WHEN PR.one_time_expense_flag = 0 THEN 'N' ELSE 'Y' END AS One_Time_Flag,
                      dbo.UDF_getRentSummaryVendor(E.expense_id, P.lease_payment_id, PR.lease_projection_id) AS vendor,
                      dbo.UDF_getRentSummaryVendorNo(E.expense_id, P.lease_payment_id, PR.lease_projection_id) as vendor_no, 
                      PS.name AS Payment_Status, PR.lease_id, PR.row_id, EC.name AS Expense_Category, P.payment_status_id, 
                      PR.lease_projection_id, E.expense_status_id, PR.posting_date, 
                      PR.scheduled_payment_date AS payment_date, PR.expense_category_id,

              CASE WHEN STDALLOC.menu_id_key IS NULL THEN 1 ELSE 2 END AS alloc_type,
              CASE WHEN STDALLOC.menu_id_key IS NULL THEN 'None' ELSE 'Standard' END AS alloc_type_desc,    

              'Check Details' as checkdetail,
              (SUM(PR.amount) OVER (PARTITION BY YEAR(PR.posting_date), PR.lease_id, E.expense_id)) as Annual
FROM         dbo.la_tbl_lease_projection AS PR INNER JOIN   
                      dbo.la_tbl_lease AS L ON PR.lease_id = L.lease_id INNER JOIN
                      dbo.la_tlu_lease_status AS LS ON L.status_id = LS.status_id INNER JOIN                      
                      dbo.la_tlu_held_by AS HB ON L.heldby_id = HB.heldby_id LEFT OUTER JOIN 
la_tbl_area_allocation AS STDALLOC on STDALLOC.area_allocation_term_id in 
(select area_allocation_term_id from la_tbl_area_allocat_term ALLOC_TERM
where start_date = (select max(start_date) from la_tbl_area_allocat_term IN_ALLOC_TERM
    where area_term_id in (select area_term_id from la_tbl_area_term TERM where PR.posting_date between TERM.start_date 
                and TERM.end_date and PR.lease_id = TERM.lease_id and TERM.isdeleted = 0)
      and PR.posting_date between IN_ALLOC_TERM.start_date and IN_ALLOC_TERM.end_date)
AND area_allocation_term_id = (select max(area_allocation_term_id) from la_tbl_area_allocat_term IN_ALLOC_TERM
    where area_term_id in (select area_term_id from la_tbl_area_term TERM where PR.posting_date >= TERM.start_date 
                and PR.posting_date between TERM.start_date and  TERM.end_date and PR.lease_id = TERM.lease_id and TERM.isdeleted = 0)
      and PR.posting_date between IN_ALLOC_TERM.start_date and  IN_ALLOC_TERM.end_date
        and IN_ALLOC_TERM.start_date = (select max(start_date) from la_tbl_area_allocat_term INN_ALLOC_TERM
    where area_term_id in (select area_term_id from la_tbl_area_term TERM where PR.posting_date >= TERM.start_date 
                and PR.posting_date between TERM.start_date and TERM.end_date and PR.lease_id  = TERM.lease_id and TERM.isdeleted = 0)
      and PR.posting_date between INN_ALLOC_TERM.start_date and INN_ALLOC_TERM.end_date))
and PR.posting_date >= ALLOC_TERM.start_date and PR.posting_date <= ALLOC_TERM.end_date
and ALLOC_TERM.isdeleted = 0)
and STDALLOC.percentage > 0

              LEFT OUTER JOIN
                      dbo.la_tbl_expense AS E ON E.expense_id = PR.expense_id LEFT OUTER JOIN
                      dbo.la_tbl_lease_payment AS P ON PR.lease_projection_id = P.lease_projection_id LEFT OUTER JOIN
                      dbo.la_tlu_payment_status AS PS ON P.payment_status_id = PS.payment_status_id LEFT OUTER JOIN
                      dbo.la_tlu_expense_category AS EC ON PR.expense_category_id = EC.expense_category_id
WHERE     (PR.IsDeleted = 0) AND (E.IsDeleted = 0 OR
                      E.IsDeleted IS NULL)
                      and HB.system_type <> '2' and HB.IsDeleted = 0

              and PR.lease_projection_id not in (select distinct(lease_projection_id) from la_tbl_lease_proj_nonstd_alloc where isdeleted = 0)
GO

1 个答案:

答案 0 :(得分:4)

您可以通过在视图中重构查询来解决此问题:

select t.*
from (<subquery1>
      union
      <subquery2>
     ) t

然后你可以这样做:

select t.*,
       SUM(amount) OVER (PARTITION BY YEAR(posting_date), lease_id, expense_id)) as Annual
from (<subquery1>
      union
      <subquery2>
     ) t

您可能必须确保子查询返回所有字段 - amount,posting_date,lease_id和expense_id。