title: SQL Server - Manipulating Values to Create a String
body: <p>I am trying to populate the column String_to_Use the way it displays on the 5th column from left to right, with values from the ID column, displaying the ranges with "-". Code below produces the last column string_to_use incorrectly.   </p>
  
  <pre><code>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;
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Can someone please help me with this? Thank you!</p>