gpt4 book ai didi

SQL close close 随着时间的推移数据中的差距

转载 作者:行者123 更新时间:2023-12-02 02:19:32 29 4
gpt4 key购买 nike

我有一个用于原型(prototype)的游戏数据表。我在工作时生成数据,但当我离开并且我的机器进入休眠状态时,数据生成停止。这导致我的元素收藏出现很大差距。

我希望能够移动表格的 DateTimeCreated 列中每个项目的值,以便任何项目与下一个生成的项目之间的间隔不会超过 10 分钟项目。

表的结构是这样的:

CREATE TABLE [dbo].[Items](
[Id] [uniqueidentifier] NOT NULL,
[DateTimeCreated] [datetimeoffset](7) NOT NULL,
[AuthorId] [uniqueidentifier] NOT NULL,
[Source] [varchar](max) NOT NULL,
[FullText] [varchar](max) NOT NULL,
CONSTRAINT [PK_Items] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

我正在考虑在 L2S 中执行此操作,但我有超过 100 万条记录,所以 IDK 如果那是最佳解决方案(迭代每个项目)。我知道必须有某种方法可以在 SQL 中执行此操作,速度会快得多。

最佳答案

另一种排名函数方法(未经过 100% 测试):

DECLARE @tenMinutes AS INT = 600;


WITH StartingPoints AS
(
SELECT DateTimeCreated, ROW_NUMBER() OVER(ORDER BY DateTimeCreated) AS rownum
FROM dbo.Items AS A
WHERE NOT EXISTS(
SELECT * FROM dbo.Items AS B
WHERE B.DateTimeCreated < A.DateTimeCreated
AND DATEDIFF(SECOND,B.DateTimeCreated, A.DateTimeCreated) BETWEEN 0 AND @tenMinutes
)
),
EndingPoints AS
(
SELECT DateTimeCreated, ROW_NUMBER() OVER(ORDER BY DateTimeCreated) AS rownum
FROM dbo.Items AS A
WHERE NOT EXISTS(
SELECT * FROM dbo.Items AS B
WHERE A.DateTimeCreated < B.DateTimeCreated
AND DATEDIFF(SECOND,A.DateTimeCreated, B.DateTimeCreated) BETWEEN 0 AND @tenMinutes
)
),
Islands AS
(
SELECT S.DateTimeCreated AS start_range,
E.DateTimeCreated AS end_range,
ROW_NUMBER() OVER(ORDER BY S.DateTimeCreated) AS row_num
FROM StartingPoints AS S
JOIN EndingPoints AS E on E.rownum = S.rownum
),
Ofs AS
(
SELECT I2.start_range,
I2.end_range,
I1.end_range AS prev,
DATEDIFF(SECOND, I1.end_range, I2.start_range) AS offset
FROM Islands AS I1
JOIN Islands AS I2 ON I2.row_num = I1.row_num + 1 OR I2.row_num IS NULL
),
CmlOfs AS
(
SELECT O1.start_range,
O1.end_range,
O1.prev,
O1.offset,
(SELECT SUM(O2.offset) FROM Ofs AS O2
WHERE O2.start_range <= O1.start_range) AS cum_offset
FROM Ofs AS O1
),
UpdateQ AS
(
SELECT Items.*, DATEADD(SECOND, -1 * CmlOfs.cum_offset, Items.DateTimeCreated) AS new_value
FROM Items
JOIN CmlOfs ON Items.DateTimeCreated BETWEEN CmlOfs.start_range AND CmlOfs.end_range
)
UPDATE UpdateQ
SET DateTimeCreated = new_value;

关于SQL close close 随着时间的推移数据中的差距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8977892/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com