【问题标题】:Allocating Balance分配余额
【发布时间】:2017-08-01 18:03:24
【问题描述】:

我在工作中遇到了一个我没有想法的问题。我不会写 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 , Google , 300 到期余额

6107 , Google , 200 后到期余额

106、阿尔塔维斯塔、资金不足

108、阿尔塔维斯塔、资金不足

512 , Yahoo , Post 40 到期余额

514、雅虎、460 后

510、雅虎、资金不足

513、雅虎、资金不足

【问题讨论】:

  • 请查看此链接(spaghettidba.com/2015/04/24/…) 了解更多关于如何改进问题的信息
  • 我不认为你可以将 PL/SQL 与 Sql-Server 一起使用,所以你的其中一个标签一定是错误的。
  • @TabAlleman 哦,我不知道! :(
  • 更好的解释是让您显示预期的输出应该是什么,并将示例数据作为文本而不是图像发布。
  • Syed 如果您费心实际阅读那篇文章,它解释了如何格式化数据,以便它可以很好地发布。由于阅读一篇关于如何让您的帖子更容易被他人阅读的文章是 TLDR(太久没有阅读),我发现您的问题 TSDH(太草率没有帮助)。

标签: sql sql-server


【解决方案1】:

您的示例输出与您的实际表格不匹配。查看您的帖子,您可以在“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`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多