简体   繁体   English

SQL Select,是否可以基于键将特定的重复行合并为1?

[英]SQL Select, is it possible to merge specific dupe rows into 1 based on a key?

I'm a little stumped on this. 我对此有些困惑。 I have a table that looks like the following: 我有一个表,如下所示:

Group_Key      Trigger_Type        Event_Type         Result_Id
1                   A                   A                 1
2                   B                   B                 2
3                   C                   C                 3 
3                   C                   C                 4
4                   E                   E                 5
5                   F                   F                 6
5                   F                   F                 7

There are rows that will have the same survey (all columns should be the same aside from result_id) key but they will have a different result_Id. 有些行将具有相同的调查(除了result_id之外,所有列都应相同)键,但是它们将具有不同的result_Id。 Is it possible to do a select on the table that grabs the rows and instead of returning 2 rows because of the result_id, it groups those ones that have dupes into a single row with the result_id being a concatenated string? 是否可以在表上进行选择以捕获行,而不是因为result_id而返回2行,而是将那些具有重复项的行组合到一行中,而result_id是连接的字符串? So for instance, return this: 因此,例如,返回以下内容:

Group_Key      Trigger_Type        Event_Type         Result_Id
1                   A                   A                 1
2                   B                   B                 2
3                   C                   C                 3,4 
4                   E                   E                 5
5                   F                   F                 6,7

Is this possible? 这可能吗?

Thank you, 谢谢,

Here's an example using a recursive CTE to replicate the functionality of string_agg() . 这是一个使用递归CTE复制string_agg()功能的示例。 This example is from the upsert scripts for execsql , and was written by Elizabeth Shea. 本示例来自execsqlupsert脚本 ,由Elizabeth Shea编写。 It will have to be modified for your particular use, substituting your own column names for the execsql variable references. 必须针对您的特定用途对其进行修改,用您自己的列名替换execsql变量引用。

if object_id('tempdb..#agg_string') is not null drop table #agg_string;
with enum as 
    (
    select
        cast(!!#string_col!! as varchar(max)) as agg_string,
        row_number() over (order by !!#order_col!!) as row_num
    from
        !!#table_name!!
    ),
agg as 
    (
    select
        one.agg_string,
        one.row_num
    from
        enum as one
    where
        one.row_num=1
    UNION ALL
    select
        agg.agg_string + '!!#delimiter!!' + enum.agg_string as agg_string,
        enum.row_num
    from 
        agg, enum
    where
        enum.row_num=agg.row_num+1
    )
select
agg_string 
into #agg_string
from agg
where row_num=(select max(row_num) from agg);

Using Gordon Linoff's hint you can GROUP BY the values that should be the same and concatenate the other values in one row using STRING_AGG : 使用Gordon Linoff的提示,您可以GROUP BY应当相同的值,并使用STRING_AGG将其他值串联在一起:

SELECT Group_Key, Trigger_Type, Event_Type, STRING_AGG(Result_Id, ',') as ResultId
FROM myTable
GROUP BY Group_Key, Trigger_Type, Event_Type

I would add the ordering of values to MicSim's solutions as follows: 我将值的顺序添加到MicSim的解决方案中,如下所示:

select Group_Key, Trigger_Type, Event_Type,
string_agg(Result_Id, ',') within group (order by Result_Id)
from survey
group by Group_Key, Trigger_Type, Event_Type

MS-SQL Server will not recognise STRING_AGG function. MS-SQL Server无法识别STRING_AGG函数。 Try stuff() as shown below: 尝试一下stuff(),如下所示:

SELECT Group_Key ,Trigger_Type, Event_Type,   
STUFF(
    (SELECT CONCAT( Result_Id , ', ') AS [text()]
    FROM [dbo].[TestTable] t2  
    WHERE t1.Group_Key = t2.Group_Key
    AND t1.Trigger_Type = t2.Trigger_Type
    AND t1.Event_Type= t2.Event_Type
    ORDER BY Group_Key ,Trigger_Type, Event_Type
    FOR XML PATH('')), 1, 0, '') AS Result_Id 
FROM [dbo].[TestTable] t1 

GROUP BY Group_Key ,Trigger_Type, Event_Type

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM