微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sql server 中遗留的字符串聚合

如何解决sql server 中遗留的字符串聚合

我有这张桌子

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   

我想要的是创建一个返回这样的数据的选择:

 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   

在表标准中,我有标准的名称和信息是否是一个范围(例如标准 3 是一个范围,我不需要在 start_value 中为它创建逗号分隔值):>

标准表:

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.property3
ORDER BY g.grid_id

我想为 start_value 创建逗号分隔值,其中除了 start_value (end_value,provider,property1,property2,property3) 之外,多行上有一个具有完全相同属性的条件,并且条件不是一个范围。我不想要第 5 行的值,即使它仍然是标准 1,因为它与第 1 行和第 2 行(在初始表中)的属性不同。我尝试使用 STUFF 和 GROUP BY 和 WITH CTE,但是因为我需要保持初始表中的顺序,所以我无法达到预期的结果。关于这个主题的其他问题比这个案例要容易一些,我的想法已经用完了,希望有人能提供一些提示..谢谢!

PS:我不能使用 STRING_AGG,因为我们的 sql Server 版本低于 2017。 :(

解决方法

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

select 
    criteria_id,STRING_AGG(start_value,',') start_value,end_value,provider,property1,property2,property3
from Tbl
group by criteria_id,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,'') start_value,property3
order by min(grid_id)
;

Share SQL code

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。