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

在 t-sql 中按日期范圍分組

grouping by date range in t-sql(在 t-sql 中按日期范圍分組)
本文介紹了在 t-sql 中按日期范圍分組的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試對該表進行查詢:

I'm trying to do a query on this table:

Id       startdate     enddate       amount
1        2013-01-01   2013-01-31      0.00
2        2013-02-01   2013-02-28      0.00
3        2013-03-01   2013-03-31      245
4        2013-04-01   2013-04-30      529
5        2013-05-01   2013-05-31      0.00
6        2013-06-01   2013-06-30      383
7        2013-07-01   2013-07-31      0.00
8        2013-08-01   2013-08-31      0.00

我想得到輸出:

2013-01-01          2013-02-28          0
2013-03-01          2013-06-30          1157
2013-07-01          2013-08-31           0

我想得到那個結果,這樣我就能知道錢什么時候開始進來,什么時候停止.我還對資金開始流入之前的月數(這解釋了第一行)和資金停止的月數感興趣(這解釋了為什么我也對 2013 年 7 月至 2013 年 8 月的第三行感興趣)).

I wanted to get that result so I would know when money started to come in and when it stopped. I am also interested in the number of months before money started coming in (which explains the first row), and the number of months where money has stopped (which explains why I'm also interested in the 3rd row for July 2013 to Aug 2013).

我知道我可以在日期上使用 min 和 max 并在金額上使用總和,但我不知道如何以這種方式劃分記錄.
謝謝!

I know I can use min and max on the dates and sum on amount but I can't figure out how to get the records divided that way.
Thanks!

推薦答案

這是一個想法(和 a小提琴一起去):

Here's one idea (and a fiddle to go with it):

;WITH MoneyComingIn AS
(
    SELECT MIN(startdate) AS startdate, MAX(enddate) AS enddate, 
        SUM(amount) AS amount
    FROM myTable
    WHERE amount > 0
)
SELECT MIN(startdate) AS startdate, MAX(enddate) AS enddate, 
    SUM(amount) AS amount
FROM myTable
WHERE enddate < (SELECT startdate FROM MoneyComingIn)
UNION ALL
SELECT startdate, enddate, amount
FROM MoneyComingIn
UNION ALL
SELECT MIN(startdate) AS startdate, MAX(enddate) AS enddate, 
    SUM(amount) AS amount
FROM myTable
WHERE startdate > (SELECT enddate FROM MoneyComingIn)

<小時>

還有一秒鐘,不使用 UNION (fiddle):

SELECT MIN(startdate), MAX(enddate), SUM(amount)
FROM
(
    SELECT startdate, enddate, amount,
    CASE 
        WHEN EXISTS(SELECT 1 
                    FROM myTable b 
                    WHERE b.id>=a.id AND b.amount > 0) THEN
            CASE WHEN EXISTS(SELECT 1 
                             FROM myTable b 
                             WHERE b.id<=a.id AND b.amount > 0) 
                 THEN 2 
                 ELSE 1 
            END
        ELSE 3
    END AS partition_no
    FROM myTable a
) x
GROUP BY partition_no

雖然我認為它假設 Id 是有序的.你可以用 ROW_NUMBER() OVER(ORDER BY startdate) 代替它.

although I suppose as written it assumes Id are in order. You could substitute this with a ROW_NUMBER() OVER(ORDER BY startdate).

這篇關于在 t-sql 中按日期范圍分組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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精品 | 国产亚洲成av人片在线观看桃 | 龙珠z在线观看 | 狠狠亚洲| 国产第1页| 国产91网站在线观看 | 男女羞羞视频大全 | www.久久99 | av福利网站 | 在线观看av网站永久 | 精品国产欧美一区二区三区成人 | 激情欧美一区二区三区中文字幕 | 亚洲精品一区二区三区中文字幕 | 日韩欧美大片在线观看 | 色姑娘综合网 | 亚洲国产精选 | 欧美国产日本一区 | 欧美特级黄色 | www.一区二区三区 | 国产一二三视频在线观看 | 国产精品亚洲欧美日韩一区在线 | 国产精品18久久久久久久 | 亚洲国产网址 | 国产视频一区二区三区四区五区 | 日韩和的一区二在线 | 精品欧美一区二区三区久久久 | 国产高清久久久 | 国产精品高 | 欧美一级二级在线观看 | 亚洲人人舔人人 | 欧美日韩一区二区三区四区 | 亚洲一区二区三区桃乃木香奈 | 夜夜干夜夜操 | 国产精品成人一区二区 | 日韩1区| 一区二区视频在线观看 |