Sql 基于2个列值连接列

Sql 基于2个列值连接列,sql,sql-server,grouping,Sql,Sql Server,Grouping,我有一张表,比如说临时表 Desc_1 Desc_2 Score a b 10 a c 10 x y 10 x z 10 a d 9 a e 9 a f 8 f g 8 我希望结果如下 a,b,c 10 x,y,z 10 a,d,e 9 a,f,g 8 所以第一条规则是一个组中的所有元素都应该有相同的分数 分组是如何发生的 1) 应该

我有一张表,比如说临时表

Desc_1 Desc_2 Score
a      b      10
a      c      10
x      y      10
x      z      10
a      d      9
a      e      9
a      f      8
f      g      8
我希望结果如下

a,b,c 10
x,y,z 10
a,d,e 9
a,f,g 8
所以第一条规则是一个组中的所有元素都应该有相同的分数

分组是如何发生的

1) 应该有一个直接关系,即a,b应该在表TEMP中

2) 如果TEMP中有条目为[(a,f)和(f,g)]或[(a,f)和(g,f))],则可能存在间接关系,即a,g可能在同一组中

请注意,对于不断的编辑,我深表歉意。

试试看

 SELECT score, DESC_1 FROM yourTable 
 UNION 
 SELECT score, DESC_2 FROM yourTable 
然后根据答案中显示的分数,使用
STUFF
XML路径

在SQLServer2005中,如何将多行合并到逗号分隔的列表中?


您可以按以下方式使用stuff和group by:

;With cte as (
    Select Desc_1 as descr, score from #desc
    union 
    Select Desc_2 as descr, score from #desc
) select Score, 
    stuff((select distinct ',' + descr from cte where score = d.score for xml path('')),1,1,'')
    from cte d
    group by Score
+-------+-------+
| Descr | Score |
+-------+-------+
| a,f,g |     8 |
| a,d,e |     9 |
| a,b,c |    10 |
+-------+-------+
输出如下:

;With cte as (
    Select Desc_1 as descr, score from #desc
    union 
    Select Desc_2 as descr, score from #desc
) select Score, 
    stuff((select distinct ',' + descr from cte where score = d.score for xml path('')),1,1,'')
    from cte d
    group by Score
+-------+-------+
| Descr | Score |
+-------+-------+
| a,f,g |     8 |
| a,d,e |     9 |
| a,b,c |    10 |
+-------+-------+
如果您使用的是SQL Server 2017或SQL Azure,则可以使用以下字符串\u agg

...cte
Select String_Agg(descr, ','), Score from cte
   group by Score

要连接的填充和XML路径:

;with s as (Select distinct score from #scores),
fs as (
select s.score, d1.Desc_1 as col
from s
left join #scores d1 on d1.score=s.score
UNION
select s.score, d2.Desc_2 as col
from s
left join #scores d2 on d2.score=s.score)
SELECT  
       Columns = STUFF((SELECT ',' + col 
                          FROM fs sc
                          where sc.score=fs.score
                        FOR XML PATH('')),1,1,'')
        ,score
  FROM fs 
  group by score
  order by score desc

这似乎正在返回所需的结果

IF OBJECT_ID('tempdb..#TestData', 'U') IS NOT NULL 
DROP TABLE #TestData;

CREATE TABLE #TestData (
    Desc_1 CHAR(1),
    Desc_2 CHAR(1),
    Score INT 
    );

INSERT #TestData (Desc_1, Desc_2, Score) VALUES
    ('a', 'b', 10),
    ('a', 'c', 10),
    ('x', 'y', 10),
    ('x', 'z', 10),
    ('a', 'd', 9),
    ('a', 'e', 9),
    ('a', 'f', 8),
    ('f', 'g', 8);

--SELECT * FROM #TestData td;

--============================================

WITH
    cte_UnPivValues AS (
        SELECT DISTINCT 
            td.Desc_1, 
            td.Score, 
            upv.UnPivValue
        FROM
            #TestData td
            CROSS APPLY ( VALUES (td.Desc_1), (td.Desc_2) ) upv (UnPivValue)
        )
SELECT 
    Descr = STUFF((
        SELECT 
            CONCAT(',', upv2.UnPivValue)
        FROM 
            cte_UnPivValues upv2
        WHERE 
            upv1.Desc_1 = upv2.Desc_1
            AND upv1.Score = upv2.Score
        ORDER BY
            upv2.UnPivValue
        FOR XML PATH ('')

    ), 1, 1, ''),
    upv1.Score
FROM
    cte_UnPivValues upv1
GROUP BY 
    upv1.Desc_1,
    upv1.Score
ORDER BY 
    upv1.Score DESC,
    Descr ASC;

你试过什么了吗?这可以用很多方式来解释。。在结果中添加列名您根据什么区分a、b、c和x、y、z?a、b、c和d、e、f呢?很抱歉评论得这么晚。我已经编辑了这个问题。希望能有帮助。10人的前四排是如何变成两排的?基于什么指标?哦,那么它们在desc_1或desc_2中有相同的元素。比如a在前两行中是公共的,x在后两行中是公共的。那么8呢?