如何隐藏重复数据?

时间:2015-10-19 08:25:18

标签: sql sql-server select stored-procedures duplicates

我试图隐藏多行中的重复数据,这是我当前的代码,我提供了输出的图像,我需要列LOGINID,EMAIL和USERGROUP中的重复项才能显示一次。另外,如何将“ROLE”列中的数据导入“USERGROUP”列?所以它看起来像最后一张图片?我正在使用SQL Server Management Studio。任何帮助都会非常感激。

SELECT LTRIM(RTRIM(HOST0149.LOGINID)) AS LOGINID,
                CASE WHEN HOST0140.EMAIL = HOST0149.LOGINID THEN NULL
                     ELSE LTRIM(RTRIM(HOST0140.EMAIL)) END AS EMAIL,
                LTRIM(RTRIM(HOST0149.USERKEY)) AS USERGROUP, 
                LTRIM(RTRIM(HOST0150.ROLE)) AS ROLE
FROM HOST0149
  LEFT JOIN HOST0140 ON HOST0149.PERSONKEY = HOST0140.PERSONKEY
  LEFT JOIN HOST0151 ON HOST0140.PERSONKEY = HOST0151.PERSONKEY
  LEFT JOIN HOST0150 ON HOST0151.ROLEKEY = HOST0150.ROLEKEY
ORDER By LOGINID

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

你的问题有点不清楚,但我认为你想要连接重复的行,而不是将它们从数据库中删除。

假设这是正确的,您可以使用xml功能执行此操作(请参阅:Concatenate many rows into a single text string?

Select distinct LOGINID, 
                EMAIL
        (
            Select HOST0150.ROLE + char(13)+char(10) + AS [text()]
            From HOST0150
            Where HOST0151.ROLEKEY = HOST0150.ROLEKEY
            ORDER BY HOST0150.ROLEKEY
            For XML PATH ('')
        ) [roles]
    FROM HOST0149
LEFT JOIN HOST0140 ON HOST0149.PERSONKEY = HOST0140.PERSONKEY
LEFT JOIN HOST0151 ON HOST0140.PERSONKEY = HOST0151.PERSONKEY
LEFT JOIN HOST0150 ON HOST0151.ROLEKEY = HOST0150.ROLEKEY
ORDER By LOGINID

答案 1 :(得分:0)

通过导出功能将文件导出到Excel中,然后只需替换数据或添加一个检查,其中重复数据删除该行..对不起我的英文