久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

減去同一表的兩行并求和

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.

這篇關于減去同一表的兩行并求和的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 91欧美| 国产美女视频一区 | 国产成人精品久久二区二区91 | 黄片毛片免费观看 | 犬夜叉在线观看 | 2019精品手机国产品在线 | 久久精品99| 日本超碰在线 | 日日欧美 | 91玖玖| 羞羞涩涩在线观看 | 欧美激情欧美激情在线五月 | 国产玖玖 | 国产精品一区二区久久 | 亚洲日本欧美 | 不卡一区二区在线观看 | 超碰在线观看97 | 久久一| 午夜二区 | 一区二区三区免费 | 黄在线免费观看 | 亚洲三级免费看 | 亚州影院 | 色射综合| 久久国内精品 | 视频三区 | 自拍偷拍亚洲一区 | 一区二区三区四区国产 | 成人精品毛片国产亚洲av十九禁 | 亚洲欧洲中文 | 亚洲国产一区二区视频 | 综合第一页 | 久久成人精品 | 亚洲人成人一区二区在线观看 | 亚洲在线免费 | 日韩美女一区二区三区在线观看 | 999免费网站| 欧美性生活免费 | 精品91久久| 99久久精品一区二区毛片吞精 | 久久一区精品 |