简体   繁体   English

从SQL Server表创建组合值的字符串

[英]Creating a string of combined values from a SQL Server table

I built a SQL query which returns the following results: 我建立了一个SQL查询,该查询返回以下结果:

ID   Number ID  IndexColumn String_To_Use   Checking_ID
0000    1   0000    1                         -2
1000    2   1000    2                         -2
1020    3   1020    3                         -2
1130    4   1130    4                         -2
1198    5   NULL    9999     NULL             NULL
1199    6   1199    5                         -2
1210    7   1210    6                         -2
1240    8   NULL    9999     NULL             NULL
1250    9   NULL    9999     NULL             NULL
1260    10  1260    7                         7
1261    11  NULL    9999     NULL             NULL
1280    12  NULL    9999     NULL             NULL
1296    13  NULL    9999     NULL             NULL
1298    14  NULL    9999     NULL             NULL
1299    15  1299    8                         8
1501    16  NULL    9999     NULL             NULL

I need to populate the column "String_To_Use" with "ID" values in such a way that If "Checking_ID" column has values -2 more than once repeating (it means user chose IDs in a range), these repeating values would be displayed as "0000-1130"; 我需要以“ ID”值填充“ String_To_Use”列,如果“ Checking_ID”列的值大于-2重复一次(这意味着用户选择了范围内的ID),则这些重复值将显示为“0000-1130”; if values -2 is not being repeated, then for example "1260". 如果未重复值-2,则为“ 1260”。 Based on this logic, the above table will contain the following values in the String_To_Use column: 基于此逻辑,上表将在String_To_Use列中包含以下值:

ID   Number ID  IndexColumn String_To_Use   Checking_ID
0000    1   0000    1       0000-1130         -2
1000    2   1000    2       0000-1130         -2                 
1020    3   1020    3       0000-1130         -2
1130    4   1130    4       0000-1130         -2
1198    5   NULL    9999    NULL              NULL
1199    6   1199    5       0000-1210         -2
1210    7   1210    6       0000-1210         -2                  
1240    8   NULL    9999    NULL              NULL
1250    9   NULL    9999    NULL              NULL
1260    10  1260    7       1260              7
1261    11  NULL    9999    NULL              NULL
1280    12  NULL    9999    NULL              NULL
1296    13  NULL    9999    NULL              NULL
1298    14  NULL    9999    NULL              NULL
1299    15  1299    8       1299              8
1501    16  NULL    9999    NULL              NULL

thank you!! 谢谢!!

You need to define groups of "adjacency". 您需要定义“邻接”组。 In this case, you can simply do a cumulative sum of the number of times that checking_id is not -2 . 在这种情况下,您可以简单地对checking_id不是-2的次数进行累加。

After that, the rest is window functions and string manipulation: 之后,剩下的就是窗口函数和字符串操作了:

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 (order by id) as grp
      from t
     ) t;

This version assumes that id is a string. 此版本假定id是一个字符串。 If it is a number, the code is easily adapted by cluttering it with cast() or convert() . 如果是数字,则可以通过使用cast()convert()使代码混乱,从而轻松地修改代码。

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                             -2        1    0000 -1210 
1000    2   1000    2                             -2        1    0000 -1210 
1020    3   1020    3                             -2        1    0000 -1210 
1130    4   1130    4                             -2        1    0000 -1210 
1198    5   NULL    9999    NULL                NULL        0    NULL
1199    6   1199    5                             -2        1    0000 -1210 
1210    7   1210    6                             -2        1    0000 -1210 
1240    8   NULL    9999    NULL                NULL        0    NULL
1250    9   NULL    9999    NULL                NULL        0    NULL
1260    10  1260    7                             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                             8         0    1299 
1501    16  NULL    9999    NULL                NULL        0    NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM