如何在SQL Server 2008中用空格/空值替换重复的单元格?

时间:2016-11-16 09:30:42

标签: sql sql-server-2008 duplicates

请参阅附图。我试图删除前5列中的重复单元格。

程序拆分列是唯一的列唯一条目,我希望保持不变,然后前5行中的第2行和第3行显示空白或空...这是可行的吗?

非常感谢任何帮助:)

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用left join和row_number窗口函数执行此操作。 在运行此查询之前,请检查列的正确性。

SELECT   T2.[Operation cancelled within 28 days]
        ,T2.[Number of new patients seen]
        ,T2.[Number of follow up patients seen]
        ,T2.[Readmission Rate]
        ,T2.[2 Week Target]
        ,T1.[Procedure Split] 
FROM
    (SELECT DISTINCT [Operation cancelled within 28 days]
                     ,[Procedure Split] 
                     ,ROW_NUMBER() OVER(ORDER BY NUM) AS RN FROM Your_table) T1

 LEFT JOIN 

    (SELECT * FROM (SELECT DISTINCT  [Operation cancelled within 28 days]
                                    ,[Number of new patients seen]
                                    ,[Number of follow up patients seen]
                                    ,[Readmission Rate]
                                    ,[2 Week Target]
                                    ,ROW_NUMBER() OVER(ORDER BY A) AS RN FROM Your_table) X 
    WHERE RN=1 ) T2
ON T1.RN = T2.RN