从两个单独的表中检索运行总计

时间:2016-10-15 07:02:04

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

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

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

表2

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); 

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

{{1}}

enter image description here

我正在努力获得这些结果,但我很难概念化解决方案。 enter image description here

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

1 个答案:

答案 0 :(得分:2)

请尝试这个,我先用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