gpt4 book ai didi

sql - 如何选择行和附近的行

转载 作者:行者123 更新时间:2023-12-02 07:16:15 24 4
gpt4 key购买 nike

SQL Fiddle

背景

我有一个需要注意的值表:

| ID      | AddedDate   |
|---------|-------------|
| 1 | 2010-04-01 |
| 2 | 2010-04-01 |
| 3 | 2010-04-02 |
| 4 | 2010-04-02 |
| 5 | NULL | <----------- needs attention
| 6 | 2010-04-02 |
| 7 | 2010-04-03 |
| 8 | 2010-04-04 |
| 9 | 2010-04-04 |
| 2432659 | 2016-06-15 |
| 2432650 | 2016-06-16 |
| 2432651 | 2016-06-17 |
| 2432672 | 2016-06-18 |
| 2432673 | NULL | <----------- needs attention
| 2432674 | 2016-06-20 |
| 2432685 | 2016-06-21 |

我想选择 AddedDate 为空的行,并且我想选择它周围的行。在此示例问题中,只要说出 ID 为 ±3 的行就足够了。这意味着我想要:

| ID      | AddedDate   |
|---------|-------------|
| 2 | 2010-04-01 | ─╮
| 3 | 2010-04-02 | │
| 4 | 2010-04-02 | │
| 5 | NULL | ├──ID values ±3
| 6 | 2010-04-02 | │
| 7 | 2010-04-03 | │
| 8 | 2010-04-04 | ─╯

| 2432672 | 2016-06-18 | ─╮
| 2432673 | NULL | ├──ID values ±3
| 2432674 | 2016-06-20 | ─╯

Note: In reality it's a table of 9M rows, and 15k need attention.

尝试

首先,我创建一个查询来构建我有兴趣返回的范围:

SELECT
ID-3 AS [Low ID],
ID+3 AS [High ID]
FROM Items
WHERE AddedDate IS NULL

Low ID High ID
------- -------
2 8
2432670 2432676

所以我最初尝试使用它确实有效:

WITH dt AS (
SELECT ID-3 AS Low, ID+3 AS High
FROM Items
WHERE AddedDate IS NULL
)
SELECT * FROM Items
WHERE EXISTS(
SELECT 1 FROM dt
WHERE Items.ID BETWEEN dt.Low AND dt.High)

但是当我在真实数据上尝试时:

  • 总行数为 900 万行
  • 15,000 个有趣
  • 子树成本为 63,318,400
  • 需要几个小时(在我放弃并取消之前)

enter image description here

可能有一种更有效的方法。

奖励阅读

最佳答案

这是使用移动最大值重写的现有逻辑:

WITH dt AS (
SELECT
ID, AddedDate,
-- check if there's a NULL within a range of +/- 3 rows
-- and remember it's ID
max(case when AddedDate is null then id end)
over (order by id
rows between 3 preceding and 3 following) as NullID
FROM Items
)
SELECT *
FROM dt
where id between NullID-3 and NullID+3

关于sql - 如何选择行和附近的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48965204/

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