这就是我的桌子的样子;
select * into temp3
from (values('306B','123456','24-25','Erin, Fromans','1','City','2024-06-03 00:00:00.000', NULL),
('306B','123456','24-25','Erin, Fromans','2','City',NULL, '2024-07-12 00:00:00.000'),
('306B','123456','24-25','Erin, Fromans','4','City','2024-08-26 00:00:00.000', NULL),
('306B','123456','24-25','Erin, Fromans','5','City',NULL, NULL),
('306B','123456','24-25','Erin, Fromans','6','City',NULL, '2024-11-15 00:00:00.000'),
('0208A','321654','24-25','Guerrazzi, Kiana','4','Ocala','2024-08-26 00:00:00.000', NULL),
('0208A','321654','24-25','Guerrazzi, Kiana','5','Ocala',NULL, NULL),
('0208A','321654','24-25','Guerrazzi, Kiana','6','Ocala',NULL, '2024-11-15 00:00:00.000'),
('108B','4399899','24-25','Obeid, Tarek','2','City','2024-10-07 00:00:00.000',NULL),
('108B','4399899','24-25','Obeid, Tarek','3','City',NULL, '2024-11-15 00:00:00.000'),
('416A','4580487','24-25','Castellano, Isabella','1','City','2024-06-03 00:00:00.000', NULL),
('416A','4580487','24-25','Castellano, Isabella','2','City',NULL, '2024-07-12 00:00:00.000'),
('416A','4580487','24-25','Castellano, Isabella','4','City','2024-08-26 00:00:00.000', NULL),
('416A','4580487','24-25','Castellano, Isabella','5','City',NULL, NULL),
('416A','4580487','24-25','Castellano, Isabella','6','City',NULL, '2024-11-15 00:00:00.000'))
as c(apt,emplid,ay,name,block,site,indate,outdate)
我的代码如下;
select t.apt, t.emplid,t.ay,t.name, t.block,t.site,
case when t.indate is not null then t.indate else (
select min(t1.indate)
from temp3 t1 where t1.apt = t.apt and t1.name=t.name and t1.ay=t.ay and t1.site=t.site
) end indate,
case when t.outdate is not null then t.outdate else (
select max(t1.outdate) from temp3 t1
where t1.apt = t.apt and t1.name=t.name and t1.ay=t.ay and t1.site=t.site
) end outdate
from temp3 t
where block='5' and t.ay='24-25'
我的代码没有显示出需要。我的代码中错误的部分是“indate”和“outdate”值。它不应该是最小值或最大值。
需要的是,如果您拉动 block=5 并且“indate”为 null,那么它应该检查第一个 block=4,如果为 null,则检查 block=3,依此类推。
“过时”也是如此,如果“过时”对于 block=5 为 null,则检查 block=6,如果为 null,则检查 block=7,依此类推..
所以数据应该看起来像;
('306B','123456','24-25','Erin, Fromans','5','City','2024-08-26 00:00:00.000', '2024-11-15 00:00:00.000'),
('0208A','321654','24-25','Guerrazzi, Kiana','5','Ocala','2024-08-26 00:00:00.000', '2024-11-15 00:00:00.000'),
('416A','4580487','24-25','Castellano, Isabella','5','City','2024-08-26 00:00:00.000', '2024-11-15 00:00:00.000'),
block
,其中 indate
/outdate
不是 NULL
。ORDER BY
t1.block DESC
因此首先选择 lower
/higher
块。LIMIT 1
或 TOP 1
(SQL Server) 仅顶部结果。SELECT t.apt, t.emplid, t.ay, t.name, t.block, t.site,
COALESCE(
t.indate,
( SELECT t1.indate FROM temp3 t1 WHERE t1.apt = t.apt AND t1.name = t.name AND t1.ay = t.ay AND t1.site = t.site AND t1.block < t.block AND t1.indate IS NOT NULL ORDER BY t1.block DESC LIMIT 1 )
) AS indate,
COALESCE(
t.outdate,
( SELECT t2.outdate FROM temp3 t2 WHERE t2.apt = t.apt AND t2.name = t.name AND t2.ay = t.ay AND t2.site = t.site AND t2.block > t.block AND t2.outdate IS NOT NULL ORDER BY t2.block ASC LIMIT 1 )
) AS outdate
FROM temp3 t
WHERE t.block = 5 AND t.ay = '24-25'
将给出以下输出:
适合 | 明确 | 好 | 名字 | 块 | 网站 | 注明 | 过时了 |
---|---|---|---|---|---|---|---|
306B | 123456 | 24-25 | 艾琳,弗罗曼斯 | 5 | 城市 | 2024-08-26 00:00:000 | 2024-11-15 00:00:000 |
0208A | 321654 | 24-25 | 格拉兹、琪亚娜 | 5 | 奥卡拉 | 2024-08-26 00:00:000 | 2024-11-15 00:00:000 |
416A | 4580487 | 24-25 | 伊莎贝拉卡斯特拉诺 | 5 | 城市 | 2024-08-26 00:00:000 | 2024-11-15 00:00:000 |