【问题标题】:Rolling Balance SQL滚动平衡 SQL
【发布时间】:2017-02-06 13:53:08
【问题描述】:

你好,我需要一些帮助 我有以下选择语句但滚动平衡似乎不起作用。

Select Distinct
        Substring(Convert(Varchar(10), M.ValueDate, 101), 0, 11) As 'Value Date'
      , base.Reference
      , 'Transaction Discription' As [Transaction Discription]
      , M.Action
      , base.Nominal
      , base.Consideration
      , (BrokerFee + CSDPFee + CSDFee + InvestorProtectionLevyFee + ExchangeFee + GuaranteedFee) As 'Transaction Fee'
      , base.VATFee As 'VAT'
      , base.SecuritiesTransferTax
      , (base.Consideration - (BrokerFee + CSDPFee + CSDFee + InvestorProtectionLevyFee + ExchangeFee + GuaranteedFee)) As [Total Consideration]
      , (Select Top (1)
                Round(Sum((base.Consideration - (BrokerFee + CSDPFee + CSDFee + InvestorProtectionLevyFee + ExchangeFee + GuaranteedFee))), 2)
         From   CashMovements T2
         Where  T2.RowId <= M.RowId
                And T2.AccountNumber = M.AccountNumber
                And T2.Action In ('W', 'D')
        ) As 'RunningTotal'
From    (Select *
         From   Instructions
         Where  instructionId In (Select  Distinct
                                            XReference
                                  From      cashmovements As CM
                                  Where     accountnumber In (Select    AccountNumber
                                                              From      CashAccounts
                                                              Where     AccountNumber In (Select Distinct
                                                                                                    AccountNumber
                                                                                          From      CrossReferences
                                                                                          Where     Class In (Select    ScripAccountId
                                                                                                              From      ScripAccounts))))
        ) As base
Inner Join CashMovements As M
        On M.XReference = base.InstructionId
Where   M.AccountNumber = '00000000006'
        And M.Action In ('W', 'D')
        And base.Consideration <> 0
        And Nominal <> 0
Group By M.ValueDate
      , base.Reference
      , M.Action
      , base.Nominal
      , base.Consideration
      , base.BrokerFee
      , base.VATFee
      , base.CSDPFee
      , base.CSDFee
      , base.InvestorProtectionLevyFee
      , base.ExchangeFee
      , base.GuaranteedFee
      , base.SecuritiesTransferTax
      , M.RowId
      , M.AccountNumber
Order By base.Reference;

它似乎只是重复值

以下是我得到的结果集:

> Total Consideration      RunningTotal
> 137.21                    137.21  
> 137.21                    137.21
> 1462.25                   1462.25
> 4406.74                   4406.74
> 1462.25                   1462.25
> 1462.25                   1462.25
> 5878.99                   5878.99

这是我想要的结果集

> Total Consideration      RunningTotal
> 137.21                    137.21  
> 137.21                    274.42
> 1462.25                   1736.67
> 4406.74                   6143.41
> 1462.25                   7605.66
> 1462.25                   9067.91
> 5878.99                   14946.9

因此,Running Total 应该随着总考虑的金额增加每一行

【问题讨论】:

  • 我通过自动格式化程序运行了您的查询,因为它侮辱了我试图阅读它的眼球。请至少尝试格式化您的查询。它甚至可能有助于突出未来的问题。
  • 基于使用的非标准语法添加了sql-server
  • 请解释您对查询的期望以及您编写的结果与您的期望有何不同
  • 提供 MCVE 可以让我们了解您的查询出错的地方(有关提示,请参阅 herehere)。

标签: sql sql-server running-balance


【解决方案1】:

如果没有看到数据和您认为是错误的,很难判断,但我认为您的子查询列应该删除前 1 条语句,并且 base.Consideration 可能需要移到选择之外。我也将四舍五入移到了外面:

  , Round(base.Consideration - (Select Sum(BrokerFee + CSDPFee + CSDFee + InvestorProtectionLevyFee + ExchangeFee + GuaranteedFee)
    From   CashMovements T2
    Where  T2.RowId <= M.RowId
    And T2.AccountNumber = M.AccountNumber
    And T2.Action In ('W', 'D')
), 2) As 'RunningTotal'

【讨论】:

  • Top(1) 是因为该结果集返回多于一行,当子查询后跟=,,
  • 如果你只返回 1 列并且它周围有一个 SUM() 怎么能返回多行?
猜你喜欢
  • 1970-01-01
  • 2021-03-04
  • 1970-01-01
  • 2015-11-22
  • 2020-09-04
  • 1970-01-01
  • 1970-01-01
  • 2021-07-29
  • 1970-01-01
相关资源
最近更新 更多