在连接其他列值时删除重复项

问题描述 投票:0回答:4

我的数据表如下(表1)

            Table1
            ------------------------------------------
            ID  | IPaddress     | Port  | Value
            ----|---------------|-------|-------------      
            1   | xx.yy.14.15   | 332   | This is good
            ------------------------------------------      
            2   | xx.yy.14.15   | 332   | I can work
            ------------------------------------------
            3   | xx.yy.12.12   | 400   | Looks ok
            ------------------------------------------
            4   | xx.yy.12.12   | 400   | can I work
            ------------------------------------------
            5   | xx.yy.12.12   | 400   | Yes, please
            -------------------------------------------
            6   | xx.yy.14.16   | 401   | How is this
            -------------------------------------------
            7   | xx.yy.14.16   | 401   | Looks ok
            -------------------------------------------
            8   | xx.yy.14.16   | 401   | can I work
            -------------------------------------------
            9   | xx.yy.14.16   | 401   | Yes, please
            -------------------------------------------

所需的结果表:

            ID  | IPaddress     | Port  | Value
            ----|---------------|-------|-----------------------------------------------------------        
            1   | xx.yy.14.15   | 332   | This is good and I can work
            --------------------------------------------------------------------------------------      
            2   | xx.yy.12.12   | 400   | Looks ok and can I work and Yes, please
            ---------------------------------------------------------------------------------------
            3   | xx.yy.14.16   | 401   | How is this and Looks ok and can I work and Yes, please
            ---------------------------------------------------------------------------------------

这是我尝试过的:

            DECLARE @VAR1 VARCHAR(50)
            DECLARE @VAR2 VARCHAR(50)

            SELECT @VAR1 = T1.VALUE,@VAR2=T2.VALUE
            FROM TABLE1 AS T1 INNER JOIN TABLE1 AS T2 ON T1.ID =T2.ID
            WHERE T1.IPADDRESS =T2.IPADDRESS

            SELECT IPADDRSS,PORT,@VAR1 + ' AND ' +@VAR2 FROM
              SELECT T1.*,
              ROW_NUMBER() OVER (PARTITION BY T1.IPADDRESS,T1.PORT ORDER BY VALUE) AS NM
              FROM TABLE1 AS T1
              )TBL
            WHERE NM = 1

但是,从上面的查询中,如果只有2个重复的行,我能够实现所需的输出(注意:这里我将IPADDRESS和PORT视为重复,而其他列不重复)

但是,当有3或4或5行有相同的IPADDRESS和PORT时,如何实现我想要的结果?请注意,具有相同IPADDRESS和PORT的行数是动态的,有时可能会超过10。那么,如何在获得理想结果的同时处理这种动态情况呢?

希望我能正确解释。请帮忙。谢谢

sql-server sql-server-2012 sql-server-2008-r2 sql-server-2016
4个回答
2
投票

刚刚看到你的评论:SQL 2017.Iuga在前面说过,我一直在研究让我感到悲伤的老派聚合技术。 SQL 2017给了我们一个早就应该使用的string_agg函数,这使得它很容易:

SELECT
   row_number() over (order by IPaddress, Port) ID
  ,IPaddress
  ,Port
  ,string_agg(Value, ' and ')
 from Table1
 group by 
   IPaddress
  ,Port

如果排序很关键,你可能需要稍微捅一下。

@ KeithL的版本也有用,有点调试......我看到你刚刚解决了。我,我只是不太喜欢XML,这就是为什么我正在研究替代品。


1
投票

这是创建分隔列表的答案(通常是逗号,但在您的情况下'和'分隔)

尝试在该列中使用一些东西

select rownumber() over (order by IPAddress,Port),IPAddress,Port
    ,stuff((select ' and ' + value
            from table t2
            where t1.IPaddress=t2.ipaddress and t1.port=t2.port
            order by ID
            for XML path(''), type
            ).value('.','nvarchar(max)')
            ,1,len(' and '),'') as verbage
from table t1
group by IPAddress,Port

这种方式的工作方式:

外部查询基本上可以在IPAddress,Port上获得分组结果

相关子查询提供与IPAddress,Port关联的每一行的分隔列表

东西逻辑是删除第一个'和'


0
投票

你可以试试这个:

select t.* from (
select ROW_NUMBER() over (partition by port order by port) rn,id,port,value=stuff ( 
                         ( select ' ' + value from @t t
                         where t.port=t1.port
                                    for xml path ('')),1,1,''

                         ) from @t t1
                         group by id,port
) t
where rn=1

0
投票
SELECT
    t1.IpAddress,t1.port,

    value = STUFF((
        SELECT ' and ' + t2.value
        FROM Table1 t2
        WHERE t1.port = t2.port
        FOR XML PATH('')
    ),2, 3, '')
FROM Table1 t1
GROUP BY t1.port,t1.IpAddress

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.