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

減去同一表的兩行并求和

Subtract two rows of same table and sum the difference(減去同一表的兩行并求和)
本文介紹了減去同一表的兩行并求和的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

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

我有這個數(shù)據(jù),我必須計算一天的總量,但我還需要減去前一天的數(shù)量,以便只計算今天(當(dāng)前日期)生產(chǎn)的數(shù)量.

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.

當(dāng)前日期是處理訂單的實(shí)際日期,上一個日期是處理該訂單的最后一天.如果我在解釋數(shù)據(jù)時不清楚,請指出我,我試過..

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:

  • 或否
  • 當(dāng)前日期
  • 上一個日期
  • CurrAmount
  • 上一個金額

那么解決您的問題將是微不足道的.給定 AnonymousTable,這個查詢生成數(shù)據(jù):

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

這里的關(guān)鍵技術(shù)是 UNION 查詢和自聯(lián)接.您的數(shù)據(jù)具有一致的日期和時間線程化",因此當(dāng)前日期和上一個日期列之間的比較是微不足道的.

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.

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

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

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標(biāo)記轉(zhuǎn)換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計算值創(chuàng)建計算值)
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屋-程序員軟件開發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 久精品视频| 爱综合| 国产一区二区三区在线 | 亚洲第一av | 一级毛片色一级 | 一区二区三区国产 | 国产精品美女久久久 | 97久久精品午夜一区二区 | 精品乱码一区二区三四区视频 | 日韩中文字幕在线视频 | 国产精品一区二区免费看 | 国产黄色小视频 | 女人夜夜春 | 二区在线视频 | 黄网站免费在线观看 | 在线看无码的免费网站 | 精品一区二区三区在线观看国产 | 日韩美香港a一级毛片免费 国产综合av | 国产成人免费视频 | 日日干夜夜操天天操 | 欧美一区二区在线免费观看 | 国产三区在线观看视频 | 国产91在线播放 | 亚洲精品乱码久久久久久按摩观 | 亚洲成人免费视频在线观看 | 日韩在线播放视频 | 99免费视频| 精品国产欧美日韩不卡在线观看 | 久久精品网 | 一区二区高清 | 日日操日日干 | 亚洲一区二区三区免费视频 | 国产精品一区二区三区在线 | 日韩国产精品一区二区三区 | 99精品国自产在线观看 | 成人免费视频网站在线观看 | 亚洲成人精 | 久久久久久av | 黑人精品欧美一区二区蜜桃 | 亚洲www.| 精品一区二区三区在线观看 |