1

我有这张桌子

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.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)之外,多行具有完全相同属性的条件,并且条件不是范围。我不想要第 5 行的值,即使它仍然是标准 1,因为它具有与第 1 行和第 2 行(在初始表中)不同的属性。我尝试使用 STUFF 和 GROUP BY 和 WITH CTE,但是因为我需要保留初始表中的订单,所以我无法达到预期的结果。关于该主题的其他问题比这个案例要容易一些,我没有想法,我希望有人能给点提示..谢谢!

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

4

1 回答 1

0

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

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)
;

分享 SQL 代码

结果:

+=============+=============+===========+==========+===========+===========+===========+
| 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)
;

分享 SQL 代码

于 2021-02-02T13:14:36.480 回答