减去同一表的两行并求和

Subtract two rows of same table and sum the difference(减去同一表的两行并求和)
本文介绍了减去同一表的两行并求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

OrNo    CurrentDate         PreviousDate        Finished    Amount
G988    02.05.2013 14:00:47 NULL                False       1560
G988    02.05.2013 21:30:00 02.05.2013 14:00:47 False       3170
G988    03.05.2013 06:00:00 02.05.2013 21:30:00 False       5095
G988    03.05.2013 07:46:24 03.05.2013 06:00:00 True        5254

表名:oldDate

我有这个数据,我必须计算一天的总量,但我还需要减去前一天的数量,以便只计算今天(当前日期)生产的数量.

I have this data, and I have to calculate the total amount on a single day but I need to also subtract the previous day's amount, so that only the amount which was produced today (current date) is calculated.

当前日期是处理订单的实际日期,上一个日期是处理该订单的最后一天.如果我在解释数据时不清楚,请指出我,我试过..

Current Date is the real date on which the order is processed and previous date is the last day's date on which this order was processed. Point me out if m not clear in explanation of the data, I tried ..

if t2.CurrentDate = t1.PreviousDate and datepart(t2.CurrentDate)= datepart(t1.CurrentDate) 

then
     if t1.CurrentDate>t2.CurrentDate

        then  @amount = t1.Amount

     else @amount = t2.Amount

我不擅长处理连接 .. :( 所以我对这个逻辑有问题,我尝试了其他示例中的其他代码但没有成功,任何想法都将不胜感激..

I'm bad at dealing with joins .. :( so I have problems with this logic, I tried some other code from other examples but was not successful, any ideas will be really appreciated..

推荐答案

如果你能得到一个带有列的表格:

If you could get a table with columns:

  • 或否
  • 当前日期
  • 上一个日期
  • CurrAmount
  • 上一个金额

那么解决您的问题将是微不足道的.给定 AnonymousTable,这个查询生成数据:

Then solving your problem would be trivial. Given the AnonymousTable, this query generates the data:

SELECT a.OrNo,
       a.CurrentDate AS CurrDate, a.PreviousDate AS PrevDate,
       a.Amount AS CurrAmount,    b.Amount AS PrevAmount
  FROM AnonymousTable AS a
  JOIN AnonymousTable AS b
    ON a.OrNo = b.OrNo AND a.PrevDate = b.CurrDate
UNION
SELECT a.OrNo,
       a.CurrentDate AS CurrDate, a.PreviousDate AS PrevDate,
       a.Amount AS CurrAmount,    0 AS PrevAmount
  FROM AnonymousTable AS a
 WHERE a.PreviousDate IS NULL

所以,大概,你可以写:

So, presumably, you could write:

SELECT OrNo, CurrDate, CurrAmount - PrevAmount AS NewAmount
  FROM (SELECT a.OrNo,
               a.CurrentDate AS CurrDate, a.PreviousDate AS PrevDate,
               a.Amount AS CurrAmount,    b.Amount AS PrevAmount
          FROM AnonymousTable AS a
          JOIN AnonymousTable AS b
            ON a.OrNo = b.OrNo AND a.PrevDate = b.CurrDate
        UNION
        SELECT a.OrNo,
               a.CurrentDate AS CurrDate, a.PreviousDate AS PrevDate,
               a.Amount AS CurrAmount,    0 AS PrevAmount
          FROM AnonymousTable AS a
         WHERE a.PreviousDate IS NULL
       )

同样清楚,如果你下定决心,你可以通过写作来简化事情:

Equally clearly, if you put your mind to it, you can simplify things by writing:

SELECT a.OrNo,
       a.CurrentDate AS CurrDate, a.PreviousDate AS PrevDate,
       a.Amount - b.Amount AS NewAmount
  FROM AnonymousTable AS a
  JOIN AnonymousTable AS b
    ON a.OrNo = b.OrNo AND a.PrevDate = b.CurrDate
UNION
SELECT a.OrNo,
       a.CurrentDate AS CurrDate, a.PreviousDate AS PrevDate,
       a.Amount AS NewAmount
  FROM AnonymousTable AS a
 WHERE a.PreviousDate IS NULL

这里的关键技术是 UNION 查询和自联接.您的数据具有一致的日期和时间线程化",因此当前日期和上一个日期列之间的比较是微不足道的.

The key techniques here are the UNION query and the self-join. Your data has consistent 'threading' of the dates and times, so the comparison between the current date and previous date columns is trivial.

这篇关于减去同一表的两行并求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
SQL query to group by day(按天分组的 SQL 查询)
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)