0

I'd really appreciate your help.

The situation is I have these two tables:

Table 1: debits. Example:

    date       item   value_debits
    2012-08-01 item1  10
    2012-08-03 item2  15

Table 2: credits. Example:

    date       item   value_credits
    2012-07-31 item3  20
    2012-08-02 item4  30  

Desired result:

    date       item   value  balance
    2012-07-31 item3  20     20
    2012-08-01 item1  (10)   10
    2012-08-02 item4  30     40
    2012-08-03 item3  (15)   25

I can easily calculate cumulative values for each of the tables separately:

set @cumulative :=0;
select date, item, value_debits, @cumulative := @cumulative + value_debits AS "Cumulated" 
from debits
order by date DESC

It's not too difficult to union and order by date these two tables to get this:

    date       item   value
    2012-07-31 item3  20
    2012-08-01 item1  10
    2012-08-02 item4  30
    2012-08-03 item3  15

But how to get to the desired result is beyond me.

Thanks in advance!

4

2 回答 2

1

您可以使用:

SELECT     a.date,
           a.item,
           CASE 
               WHEN a.value < 0 THEN CONCAT('(', a.value, ')') 
               ELSE a.value
           END AS value,
           @bal:=@bal+a.value AS balance
FROM       (
           SELECT date, item, value_debits*-1 AS value FROM debits
           UNION ALL
           SELECT date, item, value_credits FROM credits
           ) a
CROSS JOIN (SELECT @bal:=0) bal_init
ORDER BY   a.date

看看这个SQLFiddle 演示

于 2012-08-03T22:28:27.720 回答
0

这是我公认的低效尝试您的解决方案。如果有时间,我会尝试制定一个更有效的方法。但是,它确实有效。

SELECT U.`date`, U.item, U.value, 
  (SELECT SUM(U2.value) FROM 
     (SELECT `date`,item,-value_debits as value
      FROM debits
      UNION ALL
      SELECT `date`,item,value_credits as value
      FROM credits
     ) AS U2
   WHERE `date` <= U.date) 
   AS balance 
FROM 
  (SELECT `date`,item,-value_debits as value
   FROM debits
   UNION ALL
   SELECT `date`,item,value_credits as value
   FROM credits
  ) AS U
ORDER BY `date`

如果贷方和借方在同一张表中,这会稍微容易一些。

更好的解决方案

SET @total=0;
SELECT U.`date`, 
       U.item, 
       CASE WHEN U.value<0 
          THEN CONCAT('(', -U.value, ')') 
          ELSE U.value
       END as value, 
       @total:=@total+value AS balance
FROM (SELECT `date`,item,-value_debits as value
      FROM debits
      UNION ALL
      SELECT `date`,item,value_credits as value
      FROM credits
     ) AS U
ORDER BY U.`date`;
于 2012-08-03T20:48:15.227 回答