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

上次值由負變為正

Last time value changed from negative to positive(上次值由負變為正)
本文介紹了上次值由負變為正的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

如何通過 PersonID 返回上次余額字段從負值變為正值的日期?

How can you return the date by PersonID when the last time the Balance field went from a negative value to a positive value?

在下面的示例數據中,PersonID 1 發生在 2019 年 7 月 8 日,PersonID 2 發生在 2019 年 8 月 8 日.值可以多次從負數變為正數,但應僅引用最近一次發生.

In the sample data below, for PersonID 1 it happened for 8th July 2019, for PersonID 2 it happened for 8th August 2019. There can be multiple times the value changes from negative to positive but it should only reference the latest time it happens.

預期輸出:

我有以下示例數據

Create Table #temp
(
    PersonID int,
    ActionDate date,
    Balance money
)
insert into #temp
(
    PersonID,
    ActionDate,
    Balance
)
select
    1,
    '01 Jul 2019',
    -100
union all
select
    1,
    '02 Jul 2019',
    -45
union all
select
    1,
    '03 Jul 2019',
    -80
union all
select
    1,
    '04 Jul 2019',
    -20
union all
select
    1,
    '05 Jul 2019',
    40
union all
select
    1,
    '06 Jul 2019',
    -40
union all
select
    1,
    '07 Jul 2019',
    -90
union all
select
    1,
    '08 Jul 2019',
    -150
union all
select
    1,
    '09 Jul 2019',
    100
union all
select
    1,
    '10 Jul 2019',
    120
union all
select
    1,
    '11 Jul 2019',
    130
union all
select
    1,
    '12 Jul 2019',
    140
--
union all
select
    2,
    '01 Aug 2019',
    -100
union all
select
    2,
    '02 Aug 2019',
    -45
union all
select
    2,
    '03 Aug 2019',
    80
union all
select
    2,
    '04 Aug 2019',
    20
union all
select
    2,
    '05 Aug 2019',
    -40
union all
select
    2,
    '06 Aug 2019',
    -40
union all
select
    2,
    '07 Aug 2019',
    40
union all
select
    2,
    '08 Aug 2019',
    -40
union all
select
    2,
    '09 Aug 2019',
    45
union all
select
    2,
    '10 Aug 2019',
    65
union all
select
    2,
    '11 Aug 2019',
    23
union all
select
    2,
    '12 Aug 2019',
    105

推薦答案

使用 notexists 這可能會做你想做的事.它找到最后一個負余額:

This might do what you want using not exists. It finds the last negative balance:

select t.*
from #temp t
where t.balance < 0 and
      not exists (select 1
                  from #temp t2
                  where t2.personid = t.personid and
                        t2.actiondate > t.actiondate and
                        t2.balance < 0
                 );

如果你想要最后的變化,你可以使用窗口函數過濾:

If you want the last change, you can filter using window functions:

select t.*
from (select t.*,
             row_number() over (partition by personid order by actiondate desc) as seqnum
      from (select t.*,
                   lead(balance) over (partition by personid order by actiondate) as next_balance
            from #temp t
           ) t
      where t.balance < 0 and
            t.next_balance > 0
     ) t
where seqnum = 1;

這篇關于上次值由負變為正的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 日本在线免费视频 | 欧美一级视频 | 欧美一级黄| 欧美精品二区三区四区免费看视频 | 国产精品自在线 | 久久精品视频免费 | 中文字幕免费在线观看 | 日韩毛片视频 | 亚洲理论片 | 91av免费观看 | 日韩国产一区 | 欧美三级韩国三级日本三斤在线观看 | 亚洲免费在线观看视频 | 深夜福利免费 | 成人免费毛片片v | 黄色国产在线观看 | 久草久草久草 | 欧美一区二区三区四区五区 | 激情综合网站 | 国产在线视频网站 | 日韩黄色在线观看 | 免费看毛片网站 | 亚洲乱码国产乱码精品精软件 | 一区两区小视频 | 不卡的av | 精品一区二区免费视频 | 色婷婷18 | 97久久精品人人澡人人爽 | 久久久麻豆 | 综合二区 | 国产精品2区 | 中国黄色1级片 | 深夜福利免费 | 国产精品主播一区二区 | a在线播放 | 在线观看网址你懂的 | av网站免费在线观看 | 欧美日韩中文字幕 | 国产精品99久久久久久久久久久久 | 91女人18毛片水多国产 | 国产精品无遮挡 |