gpt4 book ai didi

sql - 如何用上面的行数据填充空值

转载 作者:行者123 更新时间:2023-12-01 22:19:24 26 4
gpt4 key购买 nike

我有一个示例数据

DECLARE @Table TABLE(
ID INT,
Mon VARCHAR(10),
Dt DateTime
)

INSERT INTO @Table (ID,Mon,Dt) SELECT 1, 'Jan','2016-12-23 21:08:22.280'
INSERT INTO @Table (ID,Mon,Dt) SELECT 2, 'Feb',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 3, 'Mar',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 4, 'Apr',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 5, 'May',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 6, 'Jun',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 7, 'Jul',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 8, 'Aug',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 9, 'Sep',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 10, 'Oct',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 11, 'Nov',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 12, 'Dec',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 1, 'Jan','2017-12-23 21:08:22.280'
INSERT INTO @Table (ID,Mon,Dt) SELECT 2, 'Feb',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 3, 'Mar',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 4, 'Apr',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 5, 'May',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 6, 'Jun',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 7, 'Jul',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 8, 'Aug',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 9, 'Sep',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 10, 'Oct',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 11, 'Nov',NULL
INSERT INTO @Table (ID,Mon,Dt) SELECT 12, 'Dec',NULL

从@Table中选择*

给出结果为

ID  Mon Dt
1 Jan 2016-12-23 21:08:22.280
2 Feb NULL
3 Mar NULL
4 Apr NULL
5 May NULL
6 Jun NULL
7 Jul NULL
8 Aug NULL
9 Sep NULL
10 Oct NULL
11 Nov NULL
12 Dec NULL
1 Jan 2017-12-23 21:08:22.280
2 Feb NULL
3 Mar NULL
4 Apr NULL
5 May NULL
6 Jun NULL
7 Jul NULL
8 Aug NULL
9 Sep NULL
10 Oct NULL
11 Nov NULL
12 Dec NULL

我怎么会这样

 id mon dt
1 Jan 2016-01-23 21:08:22.280
2 Feb 2016-02-23 21:08:22.280
3 Mar 2016-03-23 21:08:22.280
4 Apr 2016-04-23 21:08:22.280
5 May 2016-05-23 21:08:22.280
6 Jun 2016-06-23 21:08:22.280
7 Jul 2016-07-23 21:08:22.280
8 Aug 2016-08-23 21:08:22.280
9 Sep 2016-09-23 21:08:22.280
10 Oct 2016-10-23 21:08:22.280
11 Nov 2016-11-23 21:08:22.280
12 Dec 2016-12-23 21:08:22.280
1 Jan 2017-01-23 21:08:22.280
2 Feb 2017-02-23 21:08:22.280
3 Mar 2017-03-23 21:08:22.280
4 Apr 2017-04-23 21:08:22.280
5 May 2017-05-23 21:08:22.280
6 Jun 2017-06-23 21:08:22.280
7 Jul 2017-07-23 21:08:22.280
8 Aug 2017-08-23 21:08:22.280
9 Sep 2017-09-23 21:08:22.280
10 Oct 2017-10-23 21:08:22.280
11 Nov 2017-11-23 21:08:22.280
12 Dec 2017-12-23 21:08:22.280

建议我

最佳答案

试试这个

SELECT ID,
Mon,
Dateadd(mm, id - 1, Min(Dateadd(mm, -Month(dt) + 1, dt))OVER(partition BY rn)) dt
FROM (SELECT Row_number()OVER(partition BY Mon ORDER BY id) rn,*
FROM @Table) a
ORDER BY dt

结果:

╔════╦═════╦═════════════════════════╗
║ ID ║ Mon ║ dt ║
╠════╬═════╬═════════════════════════╣
║ 1 ║ Jan ║ 2016-01-23 21:08:22.280 ║
║ 2 ║ Feb ║ 2016-02-23 21:08:22.280 ║
║ 3 ║ Mar ║ 2016-03-23 21:08:22.280 ║
║ 4 ║ Apr ║ 2016-04-23 21:08:22.280 ║
║ 5 ║ May ║ 2016-05-23 21:08:22.280 ║
║ 6 ║ Jun ║ 2016-06-23 21:08:22.280 ║
║ 7 ║ Jul ║ 2016-07-23 21:08:22.280 ║
║ 8 ║ Aug ║ 2016-08-23 21:08:22.280 ║
║ 9 ║ Sep ║ 2016-09-23 21:08:22.280 ║
║ 10 ║ Oct ║ 2016-10-23 21:08:22.280 ║
║ 11 ║ Nov ║ 2016-11-23 21:08:22.280 ║
║ 12 ║ Dec ║ 2016-12-23 21:08:22.280 ║
║ 1 ║ Jan ║ 2017-01-23 21:08:22.280 ║
║ 2 ║ Feb ║ 2017-02-23 21:08:22.280 ║
║ 3 ║ Mar ║ 2017-03-23 21:08:22.280 ║
║ 4 ║ Apr ║ 2017-04-23 21:08:22.280 ║
║ 5 ║ May ║ 2017-05-23 21:08:22.280 ║
║ 6 ║ Jun ║ 2017-06-23 21:08:22.280 ║
║ 7 ║ Jul ║ 2017-07-23 21:08:22.280 ║
║ 8 ║ Aug ║ 2017-08-23 21:08:22.280 ║
║ 9 ║ Sep ║ 2017-09-23 21:08:22.280 ║
║ 10 ║ Oct ║ 2017-10-23 21:08:22.280 ║
║ 11 ║ Nov ║ 2017-11-23 21:08:22.280 ║
║ 12 ║ Dec ║ 2017-12-23 21:08:22.280 ║
╚════╩═════╩═════════════════════════╝

关于sql - 如何用上面的行数据填充空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41304287/

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