sql 服务器中遗留的字符串聚合

String aggregate in legacy in sql server

我有这个table

grid_id | criteria_id | start_value| end_value | provider | property1 | property2 | property3
--------|-------------|------------|-----------|----------|-----------|-----------|-----------
   1    |    1        |   3        |    NULL   | internal |    1      |    1      |     1   
   2    |    1        |   1        |    NULL   | internal |    1      |    1      |     1   
   3    |    2        |   1        |    NULL   | internal |    1      |    9      |     1   
   4    |    3        |   1        |    100    | internal |    2      |    5      |     1   
   5    |    1        |   2        |    NULL   | external |    1      |    7      |     1   

我想要创建一个 select 其中 returns 这样的数据 :

 criteria_id | start_value| end_value | provider | property1 |  property2 | property3
-------------|------------|-----------|----------|-----------|----------- |-----------
   1         |    3,1     |  NULL     | internal |     1     |     1      |     1   
   2         |     1      |  NULL     | internal |     1     |     9      |     1   
   3         |     1      |  100      | internal |     2     |     5      |     1   
   1         |     2      |  NULL     | external |     1     |     7      |     1   

在 table 标准中,我有标准的名称和是否为 运行ge 的信息(例如标准 3 是 运行ge,我不会'不需要在 start_value 中为其创建逗号分隔值):

条件table:

criteria_id | criteria_name| is_range 
------------|--------------|---------
    1       |     crit_1   |    0   
    2       |     crit_2   |    0   
    3       |     crit_3   |    1

SELECT c.criteria_name AS criteria
   ,CASE WHEN c.is_by_range = 0
      THEN (IsNull(STUFF((
        SELECT ', ' + CAST(g.start_value AS VARCHAR)
        FROM survey_reallocation_scoring_grid g1
        WHERE g.grid_id = g1.grid_id AND g.criteria_id = c.criteria_id
        FOR XML PATH('')), 1, 2, ''), ''))
    ELSE 
        CAST(g.start_value AS VARCHAR)
    END AS start_value
,g.end_value 
,g.provider 
,g.property1 
,g.property2 
,g.property3 
FROM [grid] g
INNER JOIN criteria c ON g.criteria_id = c.criteria_id
GROUP BY g.grid_id, c.criteria_name,c.criteria_reallocation_scoring_id,c.is_range,g.end_value,g.provider,g.property1,g.property2,g.property3
ORDER BY g.grid_id

我想为 start_value 创建逗号分隔值,其中除了 start_value (end_value, provider,property1,property2,property3 ) 并且条件不是 运行ge。我不想要第 5 行的值,即使它仍然是标准 1,因为它具有与第 1 行和第 2 行(在初始 table 中)不同的属性。我尝试使用 STUFF 和 GROUP BY 以及 WITH CTE,但是因为我需要保持最初 table 的顺序,所以我无法达到预期的结果。关于这个话题的其他问题比这个案例要简单一些,我 运行 没有想法,我希望有人能给出一些提示..谢谢!

PS:我无法使用STRING_AGG,因为我们的服务器版本低于2017 SQL。 :(

下面是您问题的查询答案:

select 
    criteria_id, STRING_AGG(start_value, ',') start_value, end_value, provider, property1, property2, property3
from Tbl
group by criteria_id, end_value, provider, property1, property2, property3
order by min(grid_id)
;

Share SQL code

结果:

+=============+=============+===========+==========+===========+===========+===========+
| criteria_id | start_value | end_value | provider | property1 | property2 | property3 |
+=============+=============+===========+==========+===========+===========+===========+
| 1           | 3,1         | (null)    | internal | 1         | 1         | 1         |
+-------------+-------------+-----------+----------+-----------+-----------+-----------+
| 2           | 1           | (null)    | internal | 1         | 9         | 1         |
+-------------+-------------+-----------+----------+-----------+-----------+-----------+
| 3           | 1           | 100       | internal | 2         | 5         | 1         |
+-------------+-------------+-----------+----------+-----------+-----------+-----------+
| 1           | 2           | (null)    | external | 1         | 7         | 1         |
+-------------+-------------+-----------+----------+-----------+-----------+-----------+

对于较旧的 MS SQL 服务器,您可以使用下一个解决方案:

select 
    criteria_id, 
    STUFF((
        SELECT ',' + CAST(t.start_value as varchar(10))
        FROM Tbl AS t
        WHERE
            Tbl.criteria_id = t.criteria_id
            and (Tbl.end_value = t.end_value or (Tbl.end_value is null and t.end_value is null))
            and Tbl.provider = t.provider
            and Tbl.property1 = t.property1
            and Tbl.property2 = t.property2
            and Tbl.property3 = t.property3
        FOR XML PATH('')
    ), 1, 1, '') start_value,
    end_value, provider, property1, property2, property3
from Tbl
group by criteria_id, end_value, provider, property1, property2, property3
order by min(grid_id)
;

Share SQL code