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

移動中位數(shù),T-SQL 中的模式

Moving Median, Mode in T-SQL(移動中位數(shù),T-SQL 中的模式)
本文介紹了移動中位數(shù),T-SQL 中的模式的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我使用的是 SQL Server 2012,我知道計算移動平均線非常簡單.但我需要的是獲取像這樣定義的窗口框架的模式和中位數(shù)(當前行之前的窗口為 2;月份唯一):

I am using SQL Server 2012 and I know it is quite simple to calculate moving averages. But what I need is to get the mode and the median for a defined window frame like so (with a window of 2 preceding to current row; month unique):

MONTH | CODE | MEDIAN | MODE
   1      0        0      0
   2      3        1.5    0
   3      2        2      0
   4      2        2      2
   5      2        2      2
   6      5        2      2
   7      3        3      2

如果有多個值符合模式,則選擇第一個.

If several values qualify as mode, than pick the first.

推薦答案

我徹底評論了我的代碼.閱讀我對我的模式計算的評論,讓我知道它需要調(diào)整.總的來說,這是一個相對簡單的查詢.它只是有很多丑陋的子查詢,并且有很多評論.看看:

I commented my code thoroughly. Read my comments on my Mode calculations and let me know it needs tweaking. Overall, it's a relatively simple query. It just has a lot of ugly subqueries and it has a lot of comments. Check it out:

DECLARE @Table TABLE ([Month] INT,[Code] INT);
INSERT INTO @Table
    VALUES  (1,0),
            (2,3),
            (3,2),
            (4,2), --Try commenting this out to test my special mode thingymajig
            (5,2),
            (6,5),
            (7,3);

WITH CTE
AS
(
SELECT  ROW_NUMBER() OVER (ORDER BY [Month]) row_num,
        [Month],
        CAST(Code AS FLOAT) Code
FROM @Table
)

SELECT [Month],
        Code,
        ISNULL((
                SELECT  CASE
                            --When there is only one previous value at row_num = 2, find Mean of first two codes
                            WHEN A.row_num = 2 THEN (LAG(B.code,1) OVER (ORDER BY [Code]) + B.Code)/2.0
                            --Else find middle code value of current and previous two rows
                            ELSE B.Code
                        END
                FROM CTE B 
                --How subquery relates to outer query
                WHERE B.row_num BETWEEN A.row_num - 2 AND A.row_num 
                ORDER BY B.[Code] 
                --Order by code and offset by 1 so don't select the lowest value, but fetch the one above the lowest value
                OFFSET 1 ROW FETCH NEXT 1 ROW ONLY),
        0) AS Median,
        --I did mode a little different
            --Instead of Avg(D.Code) you could list the values because with mode, 
                --If there's a tie with more than one of each number, you have multiple modes
                --Instead of doing that, I simply return the mean of the tied modes
                    --When there's one, it doesn't change anything.
                        --If you were to delete the month 4, then your number of Codes 2 and number of Codes 3 would be the same in the last row.
                        --Proper mode would be 2,3. I instead average them out to be 2.5.
        ISNULL((
                SELECT AVG(D.Code)
                FROM (
                    SELECT  C.Code,
                            COUNT(*) cnt,
                            DENSE_RANK() OVER (ORDER BY COUNT(*) DESC) dnse_rank
                    FROM CTE C
                    WHERE C.row_num <= A.row_num
                    GROUP BY C.Code
                    HAVING COUNT(*) > 1) D
                WHERE D.dnse_rank = 1),
        0) AS Mode
FROM CTE A

結果:

Month       Code                   Median                 Mode
----------- ---------------------- ---------------------- ----------------------
1           0                      0                      0
2           3                      1.5                    0
3           2                      2                      0
4           2                      2                      2
5           2                      2                      2
6           5                      2                      2
7           3                      3                      2

這篇關于移動中位數(shù),T-SQL 中的模式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應該使用什么 SQL Server 數(shù)據(jù)類型來存儲字節(jié) [])
Interpreting type codes in sys.objects in SQL Server(解釋 SQL Server 中 sys.objects 中的類型代碼)
Typeorm .loadRelationCountAndMap returns zeros(Typeorm .loadRelationCountAndMap 返回零)
MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否應該返回“1?什么時候不能投射為日期?)
Converting the name of a day to its integer representation(將一天的名稱轉換為其整數(shù)表示)
How to convert nvarchar m/d/yy to mm/dd/yyyy in SQL Server?(如何在 SQL Server 中將 nvarchar m/d/yy 轉換為 mm/dd/yyyy?)
主站蜘蛛池模板: 国产高清在线观看 | 久久欧美高清二区三区 | 亚洲精品一区二区三区免 | 三级成人片| 国产污视频在线 | 特黄特色大片免费视频观看 | 亚洲国产日本 | 国产精品久久久久一区二区三区 | 亚洲综合区| 91精品久久 | 欧美a在线看 | 国产中文视频 | 一区久久| 色婷婷精品国产一区二区三区 | 日本高清精品 | 亚洲高清视频一区二区 | 蜜桃在线视频 | 欧美一区二区在线观看 | 美女视频一区二区三区 | 人人爱干| 日本高清视频在线播放 | 国产精品美女久久久久久免费 | 国产美女一区 | 久久精品亚洲欧美日韩精品中文字幕 | 国产成人jvid在线播放 | 国产视频观看 | 亚洲精品一区二区 | 粉嫩一区二区三区国产精品 | www.国产精| а天堂中文最新一区二区三区 | 在线观看黄色大片 | 日韩中文字幕在线视频观看 | 懂色中文一区二区三区在线视频 | 亚洲欧美日韩精品久久亚洲区 | 欧美h视频 | 蜜桃免费一区二区三区 | 黄色一级片在线播放 | 国产一区二区三区在线 | 国产视频久久久久 | 毛片一级片 | 国产精品亚洲一区 |