1

我对 SQL Server 相当陌生,我正在使用一个视图,该视图是由 2 个选择语句的联合创建的,这些语句在视图中创建列并使用内连接、左外连接等)。请参阅下面的脚本。

我添加了行

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

创建一个列,显示个人每月支出的年度总和。不幸的是,因为这SUM条线在工会的两个部分,我没有得到想要的结果。当posting_date和相lease_id同时expense id,年度列中的总和应该相同。但是,即使所有这些参数都匹配,联合中的一半中的项目与联合中另一半中的项目具有不同的总和。

基本上我需要做的(我认为)是在联合完成后创建年度列,以便 sum 函数在计算时看到联合两个部分的所有项目。我正在考虑将整个东西嵌套在 a 中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
4

1 回答 1

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

您可能必须确保子查询返回所有字段——金额、发布日期、租约 ID 和费用 ID。

于 2013-06-07T16:04:40.500 回答