久久久久久久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)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 欧美精品久久久久久久久久 | 伊人一二三 | 在线视频亚洲 | 中文字幕久久精品 | 不用播放器看的av | 亚洲精品久久久久中文字幕欢迎你 | 一级毛片免费看 | 男女羞羞免费视频 | 精品国产黄a∨片高清在线 成人区精品一区二区婷婷 日本一区二区视频 | 99国内精品久久久久久久 | 成人免费视频网站在线看 | 亚洲精品国产综合区久久久久久久 | 国产成人精品免费视频大全最热 | 成人一区二 | 精品视频一区二区在线观看 | 欧美日韩三级 | 操久久久 | 国产一区二区 | 这里有精品 | www国产亚洲精品久久网站 | 国产精品精品视频一区二区三区 | 国产区精品 | 97精品视频在线观看 | 天天躁日日躁aaaa视频 | www在线视频 | 国产精品久久久久久婷婷天堂 | 久久精品青青大伊人av | 高清黄色 | 欧美一区二区免费视频 | 国产成人av一区二区三区 | 天天色天天射天天干 | 97精品久久| 免费看91 | www.日本国产 | 欧产日产国产精品视频 | www.一区二区三区 | 91精品国产91久久综合桃花 | 久久国产精品无码网站 | 欧美日韩一区二区三区不卡视频 | 韩日在线 | 一级毛毛片 |