我正在修改应收帐款账龄报告。我有四张桌子
@archg(fgc, pvd, [date], archg)
@arpay(fgc, pvd, [date], arpay)
@chg(fgc, pvd, [date], chg)
@pay(fgc, pvd, [date], pay)
我需要的是将(archg和chg)组合成等于一个总和(arpay和pay)等于一个总数。我不确定我是否会以正确的方式解决这个问题。任何帮助将不胜感激。
select distinct
vp.person_name as pvd,
lfg.FinancialGroup_Code as fgc,
select (sum(isnull(chg,0))
from @chg
where [date]<= 30
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as Charges 0-30
UNION
select (sum(isnull(archg,0))
from @archg
where [date]<= 30
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as Charges 0-30;
select (sum(isnull(chg,0))
from @chg
where [date] > 30
and [date]<= 60
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as Charges 31-60
UNION
Select (sum(isnull(archg,0))
from @archg
where [date] > 30 and [date]<= 60
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as Charges 31-60;
Select (sum(isnull(chg,0))
from @chg
where [date] > 61 and [date]<= 90
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as Charges 61-90
UNION
Select (sum(isnull(archg,0))
from @archg
where [date] > 61 and [date]<= 90
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as Charges 61-90;
Select (sum(isnull(chg,0))
from @chg
where [date] > 90 and [date]<= 120
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as Charges 91-120
UNION
Select (sum(isnull(archg,0))
from @archg
where [date] > 90 and [date]<= 120
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as Charges 91-120;
Select (sum(isnull(chg,0))
from @chg
where [date] > 120 and [date]<= 150
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as Charges 121-150
UNION
Select (sum(isnull(archg,0))
from @archg
where [date] > 120 and [date]<= 150
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as Charges 121-150;
Select (sum(isnull(chg,0))
from @chg
where [date] > 150 and [date]<= 180
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as Charges 151-180
UNION
Select (sum(isnull(archg,0))
from @archg
where [date] > 150 and [date]<= 180
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as Charges 151-180;
Select (sum(isnull(chg,0))
from @chg
where [date] > 180 and [date]<= 365
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as Charges 181-365
UNION
Select (sum(isnull(archg,0))
from @archg
where [date] > 180 and [date]<= 365
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as Charges 181-365;
Select (sum(isnull(chg,0))
from @chg
where [date] > 365 and [date]<= 730
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as Charges 366-730
UNION
Select (sum(isnull(archg,0))
from @archg
where [date] > 365and [date]<= 730
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as Charges 366-730;
Select (sum(isnull(archg,0))
from @archg
where [date] > 730
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as Charges 731+
UNION
Select (sum(isnull(archg,0))
from @archg
where [date] > 730
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as Charges 731+;
-- Select (sum(isnull(chg,0)) from @chg
-- where fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as Charges Total
Select (sum(isnull(pay,0)) from @pay
where [date]<= 30
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as pmt 0-30
UNION
Select (sum(isnull(arpay,0)) from @arpay
where [date]<= 30
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as pmt 0-30;
Select (sum(isnull(pay,0)) from @pay
where [date] > 30
and datediff(day, date, @EndDate)<= 60
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as pmt 31-60
UNION
Select (sum(isnull(arpay,0)) from @arpay
where [date] > 30 and [date]<= 60
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as pmt 31-60;
Select (sum(isnull(pay,0)) from @pay
where [date] > 61 and [date]<= 90
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as pmt 61-90
UNION
Select (sum(isnull(arpay,0)) from @arpay
where [date] > 61 and [date]<= 90
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as pmt 61-90;
Select (sum(isnull(pay,0)) from @pay
where [date] > 90 and [date]<= 120
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as pmt 91-120
UNION
Select (sum(isnull(arpay,0)) from @arpay
where [date] > 90 and [date]<= 120
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as pmt 91-120;
Select (sum(isnull(pay,0)) from @pay
where [date] > 120 and [date]<= 150
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as pmt 121-150
UNION
Select (sum(isnull(arpay,0)) from @arpay
where [date] > 120 and [date]<= 150
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as pmt 121-150;
Select (sum(isnull(pay,0)) from @pay
where [date] > 150 and [date]<= 180
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as pmt 151-180
UNION
Select (sum(isnull(arpay,0)) from @arpay
where [date] > 150 and [date]<= 180
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as pmt 151-180;
Select (sum(isnull(pay,0)) from @pay
where [date] > 180 and [date]<= 365
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as pmt 181-365
UNION
Select (sum(isnull(arpay,0)) from @arpay
where [date] > 180 and [date]<= 365
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as pmt 181-365;
Select (sum(isnull(pay,0)) from @pay
where [date] > 365 and [date]<= 730
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as pmt 366-730
UNION
Select (sum(isnull(arpay,0)) from @arpay
where [date] > 365and [date]<= 730
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as pmt 366-730;
Select (sum(isnull(arpay,0)) from @arpay
where [date] > 730
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as pmt 731+
UNION
Select (sum(isnull(arpay,0)) from @arpay
where [date] > 730
and fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as pmt 731+;
-- Select (sum(isnull(pay,0)) from @pay
-- where fgc=lfg.FinancialGroup_Code and pvd=vp.person_name) as Pmt Total
into #AR
from
@chg, @pay, @archg, @arpay
order by
vp.person_name
--select * from @chg
--select * from @pay
--select * from #AR
select
pvd as provider,
fgc,
isnull(isnull([Charges 0-30],0)-isnull([Pmt 0-30],0),0) as '0-30',
isnull(isnull([Charges 31-60],0)-isnull([Pmt 31-60],0),0) as '31-60',
isnull(isnull([Charges 61-90],0)-isnull([Pmt 61-90],0),0) as '61-90',
isnull(isnull([Charges 91-120],0)-isnull([Pmt 91-120],0),0) as '91-120',
isnull(isnull([Charges 121-150],0)-isnull([Pmt 121-150],0),0) as '121-150',
isnull(isnull([Charges 151-180],0)-isnull([Pmt 151-180],0),0) as '151-180',
isnull(isnull([Charges 181-365],0)-isnull([Pmt 181-365],0),0) as '181-365',
isnull(isnull([Charges 366-730],0)-isnull([Pmt 366-730],0),0) as '366-730',
isnull(isnull([Charges 731+],0)-isnull([Pmt 731+],0),0) as '731+'
--isnull(isnull([Charges Total],0)-isnull([Pmt Total],0),0) as 'Total'
--sum(isnull(isnull([Charges 0-30],0)-isnull([Pmt 0-30],0),0)+
--isnull(isnull([Charges 31-60],0)-isnull([Pmt 31-60],0),0)+
--isnull(isnull([Charges 61-90],0)-isnull([Pmt 61-90],0),0)+
--isnull(isnull([Charges 91-120],0)-isnull([Pmt 91-120],0),0)+
--isnull(isnull([Charges 121-150],0)-isnull([Pmt 121-150],0),0)+
--isnull(isnull([Charges 151-180],0)-isnull([Pmt 151-180],0),0)+
--isnull(isnull([Charges 181-365],0)-isnull([Pmt 181-365],0),0)+
--isnull(isnull([Charges 366-730],0)-isnull([Pmt 366-730],0),0)+
--isnull(isnull([Charges 731+],0)-isnull([Pmt 731+],0),0)) as 'Total'
from #AR
--group by pvd,fgc,
--[Charges 0-30],[Charges 31-60],[Charges 61-90],[Charges 91-120],[Charges 121-150],[Charges --151-180],
--[Charges 181-365],[Charges 366-730],[Charges 731+],[Pmt 0-30],[Pmt 31-60],[Pmt 61-90],[Pmt 91-120],
--[Pmt 121-150],[Pmt 151-180],[Pmt 181-365],[Pmt 366-730],[Pmt 731+]
--order by 1,2
DROP TABLE #AR
答案 0 :(得分:2)
你看起来对我来说非常错误,或者至少是太复杂了。你应该重新开始:分解问题并一步一步地进行。我不会完全解决问题,但这应该让你开始。
首先,您要组合2个表。好的,然后开始组合它们!例如。付款:
@pay(fgc, pvd, [date], pay)
@arpay(fgc, pvd, [date], arpay)
SELECT fgc, pvd, [date], pay from @pay
UNION
SELECT fgc, pvd, [date], arpay as pay from @arpay
与其他两个表类似。
运行查询并查看结果。您似乎想要按特定日期范围进行分组。好的,也许我们可以通过以下方式添加属性:
SELECT fgc, pvd, [date], pay,
CASE WHEN [date]<= 30 THEN '0-30'
WHEN [date]> 30 AND [date] <= 60 THEN '30-60'
-- ETC, you get the idea
ELSE '>730'
END AS daterange
from (
SELECT fgc, pvd, [date], pay from @pay
UNION
SELECT fgc, pvd, [date], arpay as pay from @arpay
) allpays
运行查询,查看结果。小警告:由于需要进行全表扫描,因此可能非常慢。但我主要是想在这里说明一种思维方式,你可以稍后进行优化。
好的,现在你有了可以分组的东西(fgc,pvd):
SELECT fgc, pvd, sum(pay) FROM (
-- insert the above subquery here
) querytogroup
group by fgc, pvd
这可以与您的实体表(lfg,vp)结合使用。等等...