我正尝试使用ID列中的值填充String_to_Use列在第5列从左到右的显示方式,并用“-”显示范围。下面的代码错误地生成最后一列string_to_use。
select
t.*,
(case
when Checking_id = -2
then min(id) over (partition by grp) + '-' + max(id) over (partition by grp)
else id
end) as string_to_use
from
(select
t.*,
sum(case when Checking_id = -2 then 1 else 0 end) over (partition by id) as grp
from
t) t
order by
id;
输出:
ID Number ID IndexColumn String_To_Use Checking_id grp string_to_use
------------------------------------------------------------------------------
0000 1 0000 1 0000-1130 -2 1 0000-1210
1000 2 1000 2 0000-1130 -2 1 0000-1210
1020 3 1020 3 0000-1130 -2 1 0000-1210
1130 4 1130 4 0000-1130 -2 1 0000-1210
1198 5 NULL 9999 NULL NULL 0 NULL
1199 6 1199 5 1199-1210 -2 1 0000-1210
1210 7 1210 6 1199-1210 -2 1 0000-1210
1240 8 NULL 9999 NULL NULL 0 NULL
1250 9 NULL 9999 NULL NULL 0 NULL
1260 10 1260 7 1260 7 0 1260
1261 11 NULL 9999 NULL NULL 0 NULL
1280 12 NULL 9999 NULL NULL 0 NULL
1296 13 NULL 9999 NULL NULL 0 NULL
1298 14 NULL 9999 NULL NULL 0 NULL
1299 15 1299 8 1299 8 0 1299
1501 16 NULL 9999 NULL NULL 0 NULL
有人可以帮我吗?谢谢!
答案 0 :(得分:0)
看看下面的查询。
我要做的是根据Number和IndexColumn之间的差异创建组。 即我的分区是基于分组的,直到达到9999 indexcol记录为止。
在那之后,我获得了该组的最大id和最小id值,并使用'-'进行串联
这里是db-fiddle链接 https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=7bd4d3a489600b58740e2f82a478726b
最后查询看起来像这样
create table t(ID varchar(10),Number1 int,ID2 varchar(10),indexcol int,String_To_Use varchar(100))
insert into t
select *
from (values
('0000',1 ,'0000',1 ,'0000-1130')
,('1000',2 ,'1000',2 ,'0000-1130')
,('1020',3 ,'1020',3 ,'0000-1130')
,('1130',4 ,'1130',4 ,'0000-1130')
,('1198',5 ,NULL ,9999,NULL )
,('1199',6 ,'1199',5 ,'1199-1210')
,('1210',7 ,'1210',6 ,'1199-1210')
,('1240',8 ,NULL ,9999,NULL )
,('1250',9 ,NULL ,9999,NULL )
,('1260',10,'1260',7 ,'1260' )
,('1261',11,NULL ,9999,NULL )
,('1280',12,NULL ,9999,NULL )
,('1296',13,NULL ,9999,NULL )
,('1298',14,NULL ,9999,NULL )
,('1299',15,'1299',8 ,'1299' )
,('1501',16,NULL ,9999,NULL )
)t(id,number1,id2,indexcol,string_to_use)
select *
,max(case when indexcol <> 9999 then id end) over(partition by Number1-indexcol)as max_val
,case when max(case when indexcol <> 9999 then id end) over(partition by Number1-indexcol)
= min(case when indexcol <> 9999 then id end) over(partition by Number1-indexcol)
then max(case when indexcol <> 9999 then id end) over(partition by Number1-indexcol)
else min(case when indexcol <> 9999 then id end) over(partition by Number1-indexcol)
+'-'+
max(case when indexcol <> 9999 then id end) over(partition by Number1-indexcol)
end as computed_string_to_use
from t
order by Number1
+------+---------+------+----------+---------------+---------+------------------------+
| ID | Number1 | ID2 | indexcol | String_To_Use | max_val | computed_string_to_use |
+------+---------+------+----------+---------------+---------+------------------------+
| 0000 | 1 | 0000 | 1 | 0000-1130 | 1130 | 0000-1130 |
| 1000 | 2 | 1000 | 2 | 0000-1130 | 1130 | 0000-1130 |
| 1020 | 3 | 1020 | 3 | 0000-1130 | 1130 | 0000-1130 |
| 1130 | 4 | 1130 | 4 | 0000-1130 | 1130 | 0000-1130 |
| 1198 | 5 | | 9999 | | | |
| 1199 | 6 | 1199 | 5 | 1199-1210 | 1210 | 1199-1210 |
| 1210 | 7 | 1210 | 6 | 1199-1210 | 1210 | 1199-1210 |
| 1240 | 8 | | 9999 | | | |
| 1250 | 9 | | 9999 | | | |
| 1260 | 10 | 1260 | 7 | 1260 | 1260 | 1260 |
| 1261 | 11 | | 9999 | | | |
| 1280 | 12 | | 9999 | | | |
| 1296 | 13 | | 9999 | | | |
| 1298 | 14 | | 9999 | | | |
| 1299 | 15 | 1299 | 8 | 1299 | 1299 | 1299 |
| 1501 | 16 | | 9999 | | | |
+------+---------+------+----------+---------------+---------+------------------------+