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

移動中位數,T-SQL 中的模式

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

問題描述

限時送ChatGPT賬號..

我使用的是 SQL Server 2012,我知道計算移動平均線非常簡單.但我需要的是獲取像這樣定義的窗口框架的模式和中位數(當前行之前的窗口為 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.

推薦答案

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

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

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

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

相關文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應該使用什么 SQL Server 數據類型來存儲字節(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(將一天的名稱轉換為其整數表示)
How to convert nvarchar m/d/yy to mm/dd/yyyy in SQL Server?(如何在 SQL Server 中將 nvarchar m/d/yy 轉換為 mm/dd/yyyy?)
主站蜘蛛池模板: 视频一区二区中文字幕 | 国产精品大片在线观看 | 亚洲日韩中文字幕一区 | 日韩在线欧美 | 国产欧美日韩综合精品一区二区 | 草久久| 久久中文字幕av | 国产91 在线播放 | www.jizzjizz| 超碰在线免费公开 | 久久久亚洲成人 | 99re视频| 日韩久久中文字幕 | 日韩一区精品 | 欧美精品a∨在线观看不卡 欧美日韩中文字幕在线播放 | 亚洲av一级毛片 | 国产高清自拍视频在线观看 | 女同久久另类99精品国产 | 国产精品久久久久久影视 | 久久精品91| 91精品国产一区二区三区 | av网站在线免费观看 | 人人射人人 | 欧美日韩成人在线观看 | 亚洲一区免费视频 | 国产精品久久久久久久久免费软件 | 成人精品国产 | 性一爱一乱一交一视频 | 羞羞在线视频 | 久久国产精品99久久久大便 | 亚洲精品一区二区在线观看 | 自拍偷拍在线视频 | 91免费看片 | 欧美精品一区二区三区四区五区 | www.婷婷| 亚洲一区二区三区国产 | 91视频精选 | a黄视频| 国产精品久久久久久久久久久久午夜片 | 久久99精品久久久久久 | 亚洲精品久久久一区二区三区 |