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

SQL Server 根據(jù)不同的標(biāo)志計(jì)算累積總和/條件運(yùn)行

SQL Server Calculate cumulative sum / conditional running total depending on different flags(SQL Server 根據(jù)不同的標(biāo)志計(jì)算累積總和/條件運(yùn)行總和)
本文介紹了SQL Server 根據(jù)不同的標(biāo)志計(jì)算累積總和/條件運(yùn)行總和的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我在 SQL Server 中有一個(gè)表,其中的數(shù)據(jù)類似于此示例.

I have a table in SQL Server with data looking like this example.

ID  Flag    Art.No  Amount
1   U   A1000   -100
2   U   B2000   -5
3   V   B2000   900
4   U   B2000   -10
5   I   B2000   50
6   U   B2000   -20
7   U   A1000   -50
8   I   A1000   1000
9   V   A1000   3600
10  U   A1000   -500
11  U   A1000   -100
12  U   A1000   -2000
13  I   A1000   2000
14  U   A1000   -1000
15  I   C3000   10000
16  U   C3000   -4000
17  U   B2000   -5
18  U   B2000   -5
19  I   B2000   40
20  V   B2000   200
21  U   A1000   -500
22  U   B2000   -50
23  U   C3000   -1000

我想根據(jù)交易計(jì)算累積價(jià)值.我的問題是該表包含 3 種類型的交易.

I want to calculate ackumulated value based on the transactions. My problem is that the table contains 3 types of transactions.

  1. 標(biāo)記 U - 銷售
  2. Flag I - 進(jìn)貨
  3. Flag V - 盤點(diǎn)

當(dāng)標(biāo)志 U 和 I 出現(xiàn)時(shí),數(shù)量代表變化出現(xiàn)Flag V時(shí)數(shù)量代表盤點(diǎn)時(shí)的總量

When Flag U and I appears the amount represent the change When Flag V appears the amount represent the total amount when stocktaking

換句話說,我想找到每個(gè) unice Art.No 的最新 V-transaction,然后添加或減去 U 和 I 交易以獲得每行的累計(jì)總和.如果沒有 V-transaction 則遍歷整個(gè)數(shù)據(jù)集.

In words I want to find the latest V-transaction for each unice Art.No and then add or subtract U and I transactions to get a cummulativ sum for each row. If there is no V-transaction go through the whole dataset.

我已經(jīng)為每個(gè)藝術(shù)制作了具有預(yù)期結(jié)果的示例.No

I have made examples with expected result for each Art.No

A1000

ID  Flag    Art.No  Amount  A1000 Example
1   U   A1000   -100    
7   U   A1000   -50 
8   I   A1000   1000    
9   V   A1000   3600    3600
10  U   A1000   -500    3100
11  U   A1000   -100    3000
12  U   A1000   -2000   1000
13  I   A1000   2000    3000
14  U   A1000   -1000   2000
21  U   A1000   -500    1500

B2000

ID  Flag    Art.No  Amount  B2000 Example
2   U   B2000   -5  
3   V   B2000   900 
4   U   B2000   -10 
5   I   B2000   50  
6   U   B2000   -20 
17  U   B2000   -5  
18  U   B2000   -5  
19  I   B2000   40  
20  V   B2000   200 200
22  U   B2000   -50 150

C3000

ID  Flag    Art.No  Amount  C3000 Example
15  I   C3000   10000   10000
16  U   C3000   -4000   6000
23  U   C3000   -1000   5000

為了在數(shù)據(jù)集中獲得更多歷史記錄,在像這樣的最新 V-transaction 之前有值會(huì)很好

To get more history in the dataset there would be nice to have values before the latest V-transaction like this

B2000

ID  Flag    Art.No  Amount  B2000 Example
2   U   B2000   -5  150
3   V   B2000   900 140
4   U   B2000   -10 140
5   I   B2000   50  190
6   U   B2000   -20 170
17  U   B2000   -5  165
18  U   B2000   -5  160
19  I   B2000   40  200
20  V   B2000   200 200
22  U   B2000   -50 150

考慮每個(gè) I 和 U 事務(wù)但忽略 V 事務(wù).

Where each I and U transaction is taken in consideration but V-transactions is ignored.

推薦答案

with cte as
 (
   select *,
      -- find the latest 'V' ID per ArtNo
      max(case when Flag = 'V' then ID end) 
      over (partition by ArtNo) as Last_V_ID
   from myTable
 )
select *,
   -- cumulative sum, but ignore all rows before the latest 'V' ID
   -- includes rows when there's no 'V' ID for this ArtNo
   sum(case when ID < Last_V_ID then null else Amount end)
   over (partition by ArtNo
         order by ID
         rows unbounded preceding)
from cte
order by ArtNo, ID

參見 Fiddle

要包含上次盤點(diǎn)之前的數(shù)據(jù)并忽略所有之前的盤點(diǎn),您可以使用以下方法:

To include the data before the last stocktaking and to ignore all previous stocktakings you can use this approach:

with cte as
 (
   select *,
      -- find the latest 'V' ID per ArtNo
      max(case when Flag = 'V' then ID end) 
      over (partition by ArtNo) as Last_V_ID
   from [dbo].[Warehouse]
 )
select *,
   -- cumulative sum, but ignore all rows before the latest 'V' ID
   -- includes rows when there's no 'V' ID for this ArtNo
   sum(case when ID < Last_V_ID then null else Amount end)
   over (partition by ArtNo
         order by ID
         rows unbounded preceding)
   -- calculate in-stock based on last 'V' ID, discarding all previous 'V' rows
  ,sum(case when (ID < Last_V_ID and Flag <> 'V')  then -Amount 
            when ID = Last_V_ID then Amount 
       end)
   over (partition by ArtNo
         order by ID 
         rows between 1 following and unbounded following)
from cte
order by ArtNo, ID

兩種計(jì)算都是互斥的,因此您可以使用 COALESCE 輕松地將它們組合起來.

Both calculations are mutually exlusive, so you can easily combine them using COALESCE.

參見 Fiddle

這篇關(guān)于SQL Server 根據(jù)不同的標(biāo)志計(jì)算累積總和/條件運(yùn)行總和的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個(gè)子標(biāo)記轉(zhuǎn)換為具有多個(gè)分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個(gè)表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計(jì)算值創(chuàng)建計(jì)算值)
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?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對(duì)?) - IT屋-程序員軟件開發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(hào)(問題和答案的組合))
主站蜘蛛池模板: 久久精品99国产精品日本 | 理伦毛片 | 在线视频一区二区 | 精品国产高清一区二区三区 | 999国产视频 | 91亚洲国产精品 | 亚洲精品乱码久久久久久9色 | 欧美激情va永久在线播放 | 日韩视频中文字幕 | 国产精品欧美一区二区 | 精品在线视频播放 | 91久久精品一区二区二区 | 欧美xxxx色视频在线观看免费 | 国产精品爱久久久久久久 | 一级黄色录像毛片 | 欧美一区二区三区在线观看视频 | 色综合天天天天做夜夜夜夜做 | 国产免费一区二区三区最新6 | 欧美成人一区二免费视频软件 | 午夜日韩| 韩日精品在线观看 | 国产不卡在线观看 | 亚洲免费av一区 | 欧美一区2区三区4区公司 | 美女国内精品自产拍在线播放 | 亚洲一区二区三区在线视频 | 欧美性生活网 | 亚洲男人网 | 91在线免费视频 | 日韩成人专区 | 日韩成人在线视频 | 久久久久高清 | 欧美1区2区 | av一级一片 | 无码一区二区三区视频 | 亚洲精品1区 | 一级黄色片免费在线观看 | 亚洲高清av| 日本亚洲一区 | 看毛片的网站 | 亚洲国产精品一区在线观看 |