Sql 语句函数

Sql function for statement

我下面有 table:

现在我需要 table 比如:

Here:If TransType='Deposit' 那么它需要在 Debit 中,如果 TransType='Withdraw' 那么它需要在 Credit 中,并且 Debit-Credit 从头到尾作为 Statement(Balance)。你能给出任何解决方案吗? 请向我询问进一步的说明。

平衡有点棘手。剩下的就是简单的 case:

select t.*,
       (case when TransType = 'Deposit' then amount end) as credit,
       (case when TransType = 'Withdraw' then amount end) as debit,
       sum(case when TransType = 'Deposit' then amount
                when TransType = 'Withdraw' then - amount
           end) over (partition by BankName order by TransDate
                     ) as balance
from t;

就我得到的问题而言,您需要 2 个带有 'where' 子句的查询。 例如"Select BankName, TransDate, TransMiti, Description from TableName where TransType = "存款"" WithDraw 也一样。 这是 link 以了解更多信息。 https://www.w3schools.com/sql/sql_where.asp