分配余额

时间:2017-08-01 18:03:25

标签: sql sql-server

我在工作中遇到了一个问题,我已经没有想法了。我不写PL / SQL,但我对学校课程有点熟悉。

这是我的表结构:

CREATE TABLE [dbo].[HateThis](
[CustID] [int] NOT NULL PRIMARY KEY CLUSTERED,
[Company_Name] [nvarchar](50) NOT NULL,
[Total_Balance_Available_For_Allocation] [INT] NOT NULL,
[TotBAL] [INT] NOT NULL,
[Due_0] [INT] NOT NULL,
[Due_1_10] [INT] NOT NULL,
[Due_11_30] [INT] NOT NULL,
[Due_31_60] [INT] NOT NULL,
[Due_61_90] [INT] NOT NULL,
[Due_91_120] [INT] NOT NULL,
[Due_121_150] [INT] NOT NULL,
[Due_151_180] [INT] NOT NULL,
[Due_180+] [INT] NOT NULL,)

INSERT INTO HateThis VALUES (6106,'Google','1000','500','150','100','50','0','0','0','0','0','0'); INSERT INTO HateThis VALUES (6107,'Google','1000','500','150','150','0','0','0','0','0','0','0'); INSERT INTO HateThis VALUES (510,'Yahoo','500','10','10','0','0','0','0','0','0','0','0'); INSERT INTO HateThis VALUES (511,'Yahoo','500','0','0','0','0','0','0','0','0','0','0'); INSERT INTO HateThis VALUES (512,'Yahoo','500','40','5','15','15','4','1','0','0','0','0'); INSERT INTO HateThis VALUES (513,'Yahoo','500','500','500','0','0','0','0','0','0','0','0'); INSERT INTO HateThis VALUES (514,'Yahoo','500','1200','1000','200','0','0','0','0','0','0','0'); INSERT INTO HateThis VALUES (106,'Alta Vista','0','50','1','1','1','1','1','1','1','1','42'); INSERT INTO HateThis VALUES (107,'Alta Vista','0','0','0','0','0','0','0','0','0','0','0'); INSERT INTO HateThis VALUES (109,'Alta Vista','0','11','11','0','0','0','0','0','0','0','0');

列信息:

  • Total_Balance_Available_For_Allocation =该公司可以分配给其下的帐户的总金额。
  • TOTBAL =该帐户的到期总额(due_0 + due_11-10 + d11-30 ...)

我要编写一个查询,以便将 Total_Balance_Available_For_Allocation 的余额分配给具有最早到期余额的帐户(如果有任何到期余额,则首先查看 Due_180 + 并且它小于可用的金额,然后该金额在此列下分配,如果余额为零,则会查看 Due_151-180 并一直持续到 Due_0

如果 Total_Balance_Available_For_Allocation 中仍有剩余资金,请查看同一公司的下一个帐户,看看是否可以将其分配到该帐户中的任何列。

这个问题很难解释。如果我能更好地解释,请告诉我。

这是我到目前为止所做的:

  when total_balance_to_allocate > abs(total_balance_available) and  totbal <=  abs(total_balance_available) and totbal = a2_currbal then 'post into a2_currnal'
  when total_balance_to_allocate > abs(total_balance_available) and  totbal <=  abs(total_balance_available) and totbal = D0_10 then 'post into D0_10'
  when total_balance_to_allocate > abs(total_balance_available) and  totbal <=  abs(total_balance_available) and totbal = D11_30 then 'post into D11_30'
  when total_balance_to_allocate > abs(total_balance_available) and  totbal <=  abs(total_balance_available) and totbal = D31_60 then 'post into D31_60'
  when total_balance_to_allocate > abs(total_balance_available) and  totbal <=  abs(total_balance_available) and totbal = D61_90 then 'post into D61_90'
  when total_balance_to_allocate > abs(total_balance_available) and  totbal <=  abs(total_balance_available) and totbal = D91_120 then 'post into D91_120'
  when total_balance_to_allocate > abs(total_balance_available) and  totbal <=  abs(total_balance_available) and totbal = D121_150 then 'post into D121_150'
  when total_balance_to_allocate > abs(total_balance_available) and  totbal <=  abs(total_balance_available) and totbal = D151_180 then 'post into D151_180'
  when total_balance_to_allocate > abs(total_balance_available) and  totbal <=  abs(total_balance_available) and totbal = D180PLUS then 'post into D180PLUS'

所需的输出如下:

CustID,Company_Name,Do_This

6106,谷歌,邮政300到期到期

6107,谷歌,发布200到期余额

106,Alta Vista,资金不足

108,Alta Vista,资金不足

512,雅虎,邮政40到达到期余额

514,Yahoo,Post 460

510,雅虎,资金不足

513,雅虎,资金不足

1 个答案:

答案 0 :(得分:0)

您的示例输出与实际表格不符。查看您的帖子,您可以在“do_this”列中确定规则,但没有解释您如何确定这些值。以及确定分配最旧余额的解释。

我认为你有更多的规则可以添加,但我希望这可以帮助你指出正确的方向。

   Select
   a.[CustID]`enter code here`
  ,a.[Company_Name]
  ,a.[Total_Balance_Available_For_Allocation]
  ,a.[TotBAL]
  ,b.TotalDue
  ,case when a.Total_balance_available_for_allocation >b.TotalDue Then 
             a.Total_balance_available_for_allocation-b.TotalDue 
        else ''
        End AllocationAmountBalance

   ,case when a.Total_balance_available_for_allocation < b.TotalDue then 
   'Insufficient Money'
        else ' '
        end BalStatus

   from .[dbo].[HateThis] a
   JOIN (select ([Due_0]+[Due_1_10]+[Due_11_30]+[Due_31_60]+[Due_61_90]+
            [Due_91_120]+[Due_121_150]+[Due_151_180]+[Due_180+]) as TotalDue
            ,[CustID]

     From [dbo].[HateThis] ) b on a.custid = b.custid
    order by a.[Company_Name], a.custid`enter code here`