【问题标题】:Partial sum and subtraction (minus) in UNION QueryUNION Query中的部分和和减法(减)
【发布时间】:2012-10-23 18:48:17
【问题描述】:

我现在有两个相似的表格(一个用于账单,另一个用于付款),我现在向用户显示来自两者的联合混合数据..

Table Bills                             
CustomerId  Amount                      
1           100                        
2           100                        
1           100                        
2           100

Table Payments
CustomerId  Amount
1           100
2           100
1           100

现在我的用户可以看到以下信息

From Customer 1
Type    CustomerId   Amount
B       1            100
P       1            -100
B       1            100
P       1            -100
TOTAL 0

From Customer 2
Type    CustomerId   Amount
B       1            100
P       1            -100
B       1            100
Total 100

使用 UNION 语句一切正常

现在我需要在每条记录上显示部分余额,以便用户在查看记录时可以记住 balanca..

像这样...(渴望)

From Customer 1
Type    CustomerId   Amount    Partial
B       1            100       100
P       1            -100      0
B       1            100       100
P       1            -100      0

我已经尝试过使用像 @Partial := @Partial + Amount 这样的变量,但它似乎首先将第一部分(账单)和其余部分(付款)相加......就像这样......

From Customer 1
Type    CustomerId   Amount    Partial
B       1            100       100
P       1            -100      200
B       1            100       200
P       1            -100      100

好像是先把账单上的所有东西都加起来,然后开始减法……有人知道怎么解决吗?

****** // 更新 // ********

这里是原始查询...

(SELECT 'Bill' as cType , b.type, b.tal , 'Customer', b.number , b.date , b.subtot, b.tax, IF(b.type='CA' or b .type='CB' or b.type='CC' or b.type='CX',b.total*-1,b.total) as total FROM bills b WHERE b.idcustomer='000140') UNION ALL (选择“付款”为 cType、“CO”、“1”、“”、c.idcash、c.date、0 ,0、-c.amount FROM cash c WHERE c.idcustomer='000140' 和(c. type='CO' 或 c.type='DM') ) 按日期升序排列;

这带来了类似的东西

Bill FX 1 客户 9 2011-02-25 0.00 0.00 100.00

付款 CO 1 37 2011-03-04 0.00 0.00 -100.00

Bill FX 1 客户 616 2011-03-23 0.00 0.00 100.00

付款 CO 1 751 2011-04-12 0.00 0.00 -100.00

Bill FX 1 客户 1267 2011-04-27 0.00 0.00 100.00

付款 CO 1 1157 2011-05-10 0.00 0.00 -100.00

Bill FX 1 客户 1974 2011-05-26 0.00 0.00 100.00

付款 CO 1 1654 2011-06-08 0.00 0.00 -100.00

然后当我尝试对 patiars 求和时...使用以下代码

设置@running_total=0; (选择 'Bill' 作为 cType 、 b.type、 b.tal 、 'Customer'、 b.number 、 b.date 、 b.subtot、 b.tax、 IF(b.type='CA' 或 b.type= 'CB' 或 b.type='CC' 或 b.type='CX',b.total*-1,b.total) 作为总计,(@running_total := @running_total + total)作为 RunningTotal 来自账单 b WHERE b.idcustomer='000140') 联合所有 (SELECT 'Payment' as cType, 'CO' , '1' , '' , c.idcash , c.date , 0 ,0 , -c.amount, (@running_total := @running_total-c.amount) AS RunningTotal FROM cash c WHERE c.idcustomer='000140' and (c.type='CO' or c.type='DM') ) 按日期排序;

结果...

Bill FX 1 客户 9 2011-02-25 0.00 0.00 100.00 100.00

付款 CO 1 37 2011-03-04 0.00 0.00 -100.00 1905.00

Bill FX 1 客户 616 2011-03-23 0.00 0.00 100.00 200.00

付款 CO 1 751 2011-04-12 0.00 0.00 -100.00 1805.00

Bill FX 1 客户 1267 2011-04-27 0.00 0.00 100.00 300.00

付款 CO 1 1157 2011-05-10 0.00 0.00 -100.00 1705.00

如您所见,似乎首先从账单中求和,然后从付款中开始减去...

【问题讨论】:

  • 您想要的是运行总计。另外,请确保您要使用 UNION 而不是 UNION ALL
  • 我想通过 MySql 查询即时完成。我可以通过应用程序的代码来完成,但也许有人知道如何解决它。我会复制更改 mysql 查询我'我现在正在使用,所以很容易理解。
  • 所以您知道UNION 会删除重复项吗?这是完全可行的,看看thisthisthis
  • SQL 表本质上是无序的。除非您知道记录的顺序,否则您无法计算总和。由于有两个不同的来源,最好是时间戳。

标签: mysql union subtraction


【解决方案1】:

以下是我能想到的在 MySQL 中执行此操作的唯一方法,假设您有一个用于对记录进行排序的时间戳。如果您的数据量适中,这可能证明效率太低:

select t.*,
       ((select sum(amount)
         from bills b
         where b.customerId = t.customerId and
               b.datetime <= t.datetime
        ) -
        (select sum(amount)
         from payments p
         where p.customerid = t.customerid and
               p.datetime <= t.datetime
        )
       ) as balance
from ((select 'B' as type, customerid, amount, datetime from bills b) union all
      (select 'P', customerid, amount, datetime from payments p)
     ) t

【讨论】:

  • 我在这里粘贴原始查询(不求和)
  • (SELECT 'Bill' as cType , b.type, b.tal , b.name, b.number , b.date , b.subtot, b.tax, IF(b.type= 'CA' 或 b.type='CB' 或 b.type='CC' 或 b.type='CX',b.total*-1,b.total) 作为来自账单 b 的总金额,其中 b.idcustomer=' 000140') UNION ALL (SELECT 'Payment' as cType, 'CO' , '1' , '' , c.idcash , c.date , 0 ,0 , -c.amount FROM cash c WHERE c.idcustomer='000140 ' 和( c.type='CO' 或 c.type='DM') )按日期升序排列;
  • 在原始帖子中,您可以在文本末尾看到...原始查询和结果以及使用 running_total 查询...
  • 请记住,结果将始终是 20-100 条记录(没有性能开销)
猜你喜欢
  • 1970-01-01
  • 2015-08-17
  • 2020-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-27
  • 2019-10-31
  • 1970-01-01
相关资源
最近更新 更多