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

如何插入每兩行的減法并將其插入新列

how to insert subtract of each two subsequent rows and inserting it into a new column(如何插入每兩行的減法并將其插入新列)
本文介紹了如何插入每兩行的減法并將其插入新列的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個關于在 sql 中編寫查詢的問題.

在圖片 1 中,我想從 row1(在列日期中)減去第 2 行,并將其結果插入到標題為 Recency 的新列的 row1 中.并再次從第 2 行減去第 3 行并將其插入新列的第 2 行,依此類推.

:

..........................................................................................

和其他問題:

我還想計算每個用戶在當前日期之前的活動頻率.我想計算每一行的頻率.例如對于這個例子,對于用戶 abkqz,我們有:

用戶名頻率abkqz 4abkqz 3abkqz 2abkqz 10

解決方案

假設如下表結構

CREATE TABLE [15853354] -- 堆棧溢出問題編號([用戶名] VARCHAR(20),[提交] INT,[日期] 日期,[得分] NUMERIC(9,2),[點數] NUMERIC(9,1))插入 [15853354]價值觀('abkqz', 5, '12 JUL 2010', 83.91, 112.5),('abkqz', 5, '9 JUN 2010', 77.27, 0),('abkqz', 5, '17 May 2010', 91.87, 315)

然后你可以寫下面的查詢

;WITH [cte15853354] AS(選擇[用戶名],[提交],[日期],[分數],【積分】,ROW_NUMBER() OVER (ORDER BY [user-name], [date] DESC) AS [ROWNUMBER]來自 [15853354])選擇t.[用戶名],t.[提交],DATEDIFF(DAY, ISNULL([t-1].[date],t.[date]),t.[date]) AS [recency],t.[得分],t.[點數]從 [cte15853354] t左連接 [cte15853354] [t-1]ON [t-1].[用戶名] = t.[用戶名]AND [t-1].[ROWNUMBER] = t.[ROWNUMBER] + 1

這使用公用表表達式來計算行號,然后進行自聯接以將每一行與下一行聯接,然后計算以天為單位的日期差異.

結果如下:

I have a question about writing query in sql.

in the picture 1 I want to subtract row 2 from row1 (in column date) and insert it's result in row1 of new column with the title of Recency. and again subtract row3 from row2 and insert it in row2 of the new column, and so on.

picture 1:

in fact I want to calculate the recency of each user's activity. for example in the following picture, I calculated this for one user(manually); I want to do this for all of the users by writing a query in sql.

picture 2:

..........................................................................................

and other question:

I also want to calculate the frequency of activity of each user before the current date. I want to calculate frequency for each row. for example for this example, for user abkqz we have:

user name     frequency
abkqz             4
abkqz             3
abkqz             2
abkqz             1
abkqz             0

解決方案

Assuming the following table structure

CREATE TABLE [15853354] -- Stack Overflow question number
(
    [user-name] VARCHAR(20),
    [submissions] INT,
    [date] DATE,
    [score] NUMERIC(9,2),
    [points] NUMERIC(9,1)
)

INSERT [15853354]
VALUES
    ('abkqz', 5, '12 JUL 2010', 83.91, 112.5),
    ('abkqz', 5, '9 JUN 2010', 77.27, 0),
    ('abkqz', 5, '17 MAY 2010', 91.87, 315)

Then you could write the following query

;WITH [cte15853354] AS
(
    SELECT 
        [user-name],
        [submissions],
        [date],
        [score],
        [points],
        ROW_NUMBER() OVER (ORDER BY [user-name], [date] DESC) AS [ROWNUMBER]
    FROM [15853354]
)
SELECT 
    t.[user-name],
    t.[submissions],
    DATEDIFF(DAY, ISNULL([t-1].[date],t.[date]),t.[date]) AS [recency],
    t.[score],
    t.[points]
FROM [cte15853354] t
LEFT JOIN [cte15853354] [t-1]
    ON [t-1].[user-name] = t.[user-name]
    AND [t-1].[ROWNUMBER] = t.[ROWNUMBER] + 1

This uses a Common Table Expression to calculate a row number, and then does a self join to join each row with the next, and then calculates the date difference in days.

This is the result:

這篇關于如何插入每兩行的減法并將其插入新列的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 国产欧美精品一区二区色综合朱莉 | 亚洲一区二区三区视频 | 在线观看视频中文字幕 | 国产精品高清在线 | 色视频网站 | 先锋资源站 | 狠狠色综合久久婷婷 | 羞羞视频在线观看 | 免费性视频 | 日韩午夜在线播放 | 日韩一级免费 | 成人性视频在线播放 | 中文字幕一区二区三区四区五区 | 国产精品一区二区久久 | 特级做a爰片毛片免费看108 | 久久99视频免费观看 | 久久er99热精品一区二区 | 日韩精品一区二区三区中文在线 | 在线观看免费毛片 | 高清国产午夜精品久久久久久 | 日韩精品av| 玖玖视频网 | 日韩国产精品一区二区三区 | 综合久久一区 | 婷婷综合网| 日韩免费av | 久久久久久久久综合 | 亚洲欧洲激情 | 免费在线黄色av | 欧美日韩亚洲国产综合 | 国产婷婷精品 | 狠狠操婷婷 | 在线视频91| 中文字幕 亚洲一区 | 国产成人在线视频播放 | 丝袜 亚洲 另类 欧美 综合 | 成人精品视频 | 一级特黄网站 | 亚洲国产精品久久久 | 免费一区 | 国产精品揄拍一区二区 |