【问题标题】:Need help in creating ledger from table需要帮助从表创建分类帐
【发布时间】:2016-03-23 14:05:16
【问题描述】:

每个人..我只是被困在我有两个如下表的情况下。第一个表是 tblCharge,第二个是 tblPayment

chargeId   Plan   TotalAmount
 1         A        400
 2         B        200
 3         C        300 


PaymentId  ChargeId  PayAmount
 1            1       100
 2            1       50
 3            1       70
 4            1       120
 5            1       10
 6            2       50
 7            2       70

我希望通过加入上表来得到如下输出。应从每一行的支付金额中减去总金额。

Plan    Amount  Pay
A       400     100
A       300     50
A       250     70
A       180     120
A       60      10

【问题讨论】:

  • 你自己写过代码吗?你能分享你的代码吗?
  • Select tblCharge.strCharge [Plan] ,tblCharge.fltChargeAmount-tblPaymentDetail.fltAmount [Amount] ,tblPaymentDetail.fltAmount [Amount Pay] from tblCharge inner join tblPayment on tblCharge.ChargeId=tblPaymentDetail.intChargeId

标签: sql-server sql-server-2012


【解决方案1】:

使用SUM OVER():

SQL Fiddle

SELECT
    c.ChargeId,
    Amount = TotalAmount 
                - SUM(PayAmount) OVER(PARTITION BY c.[Plan] ORDER BY p.PaymentId) 
                + PayAmount,
    p.PayAmount
FROM tblCharge c
INNER JOIN tblPayment p
    ON p.ChargeId = c.ChargeId

【讨论】:

    【解决方案2】:
    -- sample table
    declare @tblCharge table
    (
        ChargeId    int,
        [Plan]      char,
        TotalAmount int
    )
    
    declare @tblPayment table
    (
        PaymentId   int,
        ChargeId    int,
        PayAmount   int
    )
    
    -- sample data
    insert into @tblCharge select 1, 'A', 400
    insert into @tblCharge select 2, 'B', 200
    insert into @tblCharge select 3, 'C', 300
    
    insert into @tblPayment select 1, 1, 100
    insert into @tblPayment select 2, 1, 50
    insert into @tblPayment select 3, 1, 70
    insert into @tblPayment select 4, 1, 120
    insert into @tblPayment select 5, 1, 10
    insert into @tblPayment select 6, 2, 50
    insert into @tblPayment select 7, 2, 70
    
    -- the query
    select  c.[Plan], 
        c.TotalAmount - isnull(a.Amt, 0) as Amount,
        p.PayAmount as Pay
    from    @tblCharge c
        inner join @tblPayment p    on  c.ChargeId  = p.ChargeId
        cross apply
        (
            select  sum(x.PayAmount) as Amt
            from    @tblPayment x
            where   x.ChargeId  = c.ChargeId
            and x.PaymentId < p.PaymentId
        ) a
    order by c.ChargeId, p.PaymentId
    

    【讨论】:

      【解决方案3】:

      您可以使用以下查询来获得所需的结果

          select chargeid,
          totalamount-isnull(lag(amount) over(partition by chargeid order by chargeid),0) as amount,
          payamount as pay
      from (
          select t2.chargeid
              ,t1.totalamount
              ,sum(t2.payamount) over (
                  partition by t2.chargeid order by t2.paymentid
                  ) as amount,
                  t2.payamount
          from tblCharge t1
          join tblpayment t2 on t1.chargeid = t2.chargeid
          ) a
      

      【讨论】:

        【解决方案4】:

        希望这个简单的查询能解决您的问题。

        SELECT C.[Plan],C.TotalAmount - ISNULL((SELECT SUM(PT.PayAmount) FROM tblPayment PT 
            WHERE PT.ChargeId = C.ChargeId AND PT.PaymentId < P.PaymentId),0) Amount,P.PayAmount Pay
        FROM    tblCharge C
        INNER JOIN tblPayment P ON P.ChargeId = C.ChargeId
        

        【讨论】:

          猜你喜欢
          • 2020-03-06
          • 1970-01-01
          • 2011-08-02
          • 2013-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-27
          • 2019-06-12
          相关资源
          最近更新 更多