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

獲取 SQL 表列的總和,直到總和達到 5000

Getting Sum of an SQL table column until the sum reaches 5000(獲取 SQL 表列的總和,直到總和達到 5000)
本文介紹了獲取 SQL 表列的總和,直到總和達到 5000的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在對具有兩列 AmountDate 的表進行 sql 查詢,該表應返回 Amount 列值的總和,直到達到5000 并且它還應該返回 Date 列中 Sum(Amount) 達到 5000 按 <排序的值代碼>日期

I am working on an sql query for a table with two columns Amount and Date which should return sum of Amount column values until reaches 5000 and it should also return the value in Date column at which Sum(Amount) reaches 5000 sorted by Date

例如,我的 SQL TABLE

   ID Amount  Date
    1 1000    5/5/2014
    2 1000    5/1/2014
    3 900     5/3/2014
    4 1500    5/4/2014
    5 2000    5/4/2014 
    6 2500    5/5/2014

在上表中,Amount 的總和是按 Date 排序后計算的,當達到 5000 時,返回 Amount 及其關聯 Date 的總和.

In the above table the sum of Amount should be calculated after sorting it by Date and should return the sum of Amount and its associated Date once it reaches 5000 mark.

對數據進行排序后,它變成如下所示

after sorting the data it becomes something like following

   ID Amount  Date    
    2 1000    5/1/2014
    3 900     5/3/2014
    4 1500    5/4/2014
    5 2000    5/4/2014 
    1 1000    5/5/2014
    6 2500    5/5/2014

查詢應該返回以下結果

TotalAmount  Date
5400         5/4/2014

以上結果是因為ID=5 Amount=200 and Date=5/4/2014

我可以知道在 SQL Server

推薦答案

對于 SQL Server,你可以使用 SUM() OVER() 并且只得到總和 >= 的第一行5000;

For SQL Server, you can use SUM() OVER() and just get the first row where the total sum is >= 5000;

WITH cte AS (
  SELECT id, date, SUM(amount) OVER (ORDER BY date,id) totalamount 
  FROM mytable
)
SELECT TOP 1 totalamount, date 
FROM cte 
WHERE totalamount >= 5000 
ORDER BY date, id;

用于測試的 SQLfiddle.

這篇關于獲取 SQL 表列的總和,直到總和達到 5000的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應該使用什么 SQL Server 數據類型來存儲字節 [])
Interpreting type codes in sys.objects in SQL Server(解釋 SQL Server 中 sys.objects 中的類型代碼)
Typeorm Does not return all data(Typeorm 不返回所有數據)
Typeorm .loadRelationCountAndMap returns zeros(Typeorm .loadRelationCountAndMap 返回零)
How to convert #39;2016-07-01 01:12:22 PM#39; to #39;2016-07-01 13:12:22#39; hour format?(如何將“2016-07-01 01:12:22 PM轉換為“2016-07-01 13:12:22小時格式?)
MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否應該返回“1?什么時候不能投射為日期?)
主站蜘蛛池模板: 国产一区二区三区在线 | 亚洲中午字幕 | 欧美日韩中文字幕在线 | 巨大荫蒂视频欧美另类大 | 亚洲成av人片在线观看 | av中文字幕在线观看 | 免费av观看| 亚洲成人一区 | 欧美中文在线 | 国产精品亚洲综合 | 一区二区三区中文字幕 | 在线一级片 | 精品国产乱码久久久久久牛牛 | 国产精品一区二 | 欧美专区在线观看 | 在线一区 | 国产精品久久国产精品99 gif | 国产精品第2页 | 精品欧美一区二区三区久久久小说 | 亚洲国产成人精品女人久久久 | 亚洲综合在线视频 | 亚洲成人精品 | 国产亚洲成av人在线观看导航 | 91一区二区三区在线观看 | 久久久久久黄 | 中文字字幕一区二区三区四区五区 | 在线小视频 | 国产999精品久久久影片官网 | 国产精品亚洲第一区在线暖暖韩国 | 成人网视频 | 午夜天堂精品久久久久 | 99爱在线免费观看 | 99re| 亚洲精品区 | 国产在线一区二区三区 | 在线免费观看视频你懂的 | 可以免费观看的av片 | 国产清纯白嫩初高生视频在线观看 | 一区二区三区欧美 | 国产精品九九视频 | 中文字幕在线观看一区 |