【问题标题】:Retrieving running totals from two separate tables从两个单独的表中检索运行总计
【发布时间】:2016-10-15 07:02:03
【问题描述】:

我正在尝试基于两个表执行运行总查询,但我有点难过。这是我到目前为止所拥有的。首先,让我为你们提供表格的 DDL 和我正在使用的示例数据。 表一

create table Actuals
(
f_year varchar(02),
f_period varchar(02),
f_fund varchar(06),
f_org varchar(06),
f_pror varchar(06),
f_trans_amt decimal

);

表 2

create table Actuals
(
f_year varchar(02),
f_period varchar(02),
f_fund varchar(06),
f_org varchar(06),
f_pror varchar(06),
f_trans_amt decimal

);

    GO
    SET ANSI_PADDING OFF
    GO
    INSERT [dbo].[Actuals] ([f_year], [f_period], [f_fund], [f_org], [f_pror], [f_trans_amt]) VALUES (N'07', N'01', N'aaa', N'bbb', N'ccc', CAST(20 AS Decimal(18, 0)))
    GO
    INSERT [dbo].[Actuals] ([f_year], [f_period], [f_fund], [f_org], [f_pror], [f_trans_amt]) VALUES (N'07', N'02', N'aaa', N'bbb', N'ccc', CAST(30 AS Decimal(18, 0)))
    GO
    INSERT [dbo].[Actuals] ([f_year], [f_period], [f_fund], [f_org], [f_pror], [f_trans_amt]) VALUES (N'07', N'03', N'aaa', N'bbb', N'ccc', CAST(50 AS Decimal(18, 0)))
    GO
    INSERT [dbo].[Actuals] ([f_year], [f_period], [f_fund], [f_org], [f_pror], [f_trans_amt]) VALUES (N'07', N'04', N'aaa', N'bbb', N'ccc', CAST(150 AS Decimal(18, 0)))
    GO
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[budget] ([f_year], [f_period], [f_fund], [f_org], [f_pror], [f_trans_amt]) VALUES (N'07', N'03', N'aaa', N'bbb', N'ccc', CAST(150 AS Decimal(18, 0)))
GO
INSERT [dbo].[budget] ([f_year], [f_period], [f_fund], [f_org], [f_pror], [f_trans_amt]) VALUES (N'07', N'06', N'aaa', N'bbb', N'ccc', CAST(150 AS Decimal(18, 0)))
GO

这些是我当前使用以下查询的结果。

with cte_actuals(
totalActual,
f_year,
f_period,
f_fund,
f_org,
f_pror
)
 as (
select sum(f_trans_amt) over (partition by f_fund,f_org,f_pror order by f_period, f_year ) totalActual,
f_year,
f_period,
f_fund,
f_org,
f_pror
from Actuals),

cte_budget (
totalBudget,
f_year,
f_period,
f_fund,
f_org,
f_pror
)
 as (
select sum(f_trans_amt) over (partition by f_fund,f_org,f_pror order by f_period, f_year ) totalBudget,
f_year,
f_period,
f_fund,
f_org,
f_pror
from budget)
select  
b.totalBudget,
a.totalActual,
a.f_year,
a.f_period,
a.f_fund,
a.f_org,
a.f_pror
From
cte_actuals a
full outer join cte_budget b on( a.f_fund = b.f_fund
 and a.f_org = b.f_org
 and a.f_pror = b.f_pror
 and a.f_year = b.f_year
and a.f_period = b.f_period
and a.f_year = b.f_year); 

我正在尝试获得这些结果,但我很难将解决方案概念化。

我的最终目标是将两个运行总计加入一个查询,但表不完全匹配。换句话说,不是每个 f_period 和 f_year 都在两个表中,所以我只能用上一期的运行总计来填补空白。上图显示了我想要完成的最终结果。

【问题讨论】:

    标签: sql sql-server sql-server-2012 sql-server-2008-r2


    【解决方案1】:

    请试试这个,我先加入cte中的表格,然后计算总和。

    ;with cte as(
    select 
         Coalesce(a.f_year, b.f_year) as f_year
        ,coalesce(a.f_period, b.f_period) as f_period
        ,coalesce(a.f_fund, b.f_fund) as f_fund
        ,coalesce(a.f_org, b.f_org) as f_org
        ,coalesce(a.f_pror, b.f_pror) as f_pror
        , Coalesce(a.f_trans_amt, 0) as ActualAmount
        ,coalesce(b.f_trans_amt, 0) as BudgetAmount
     from Actuals as a
     full outer join Budget as b on 
    ( a.f_fund = b.f_fund
     and a.f_org = b.f_org
     and a.f_pror = b.f_pror
     and a.f_year = b.f_year
    and a.f_period = b.f_period
    and a.f_year = b.f_year)
    ) select *
    ,sum(ActualAmount) over (partition by f_fund,f_org,f_pror order by f_period, f_year ) as ActualAmount
    ,sum(BudgetAmount) over (partition by f_fund,f_org,f_pror order by f_period, f_year ) as BudgetAmount
    
     from cte
    

    【讨论】:

    • 你我的朋友真是个天才!感谢优雅的解决方案。我真的被困在那里一段时间。我当然很感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    相关资源
    最近更新 更多