小编典典

如果存在重复的行,如何在sql中显示空白记录

sql

我的SQL查询结果

EmployeeID    EmployeeName    Department
1             John            Sales
2             Robert          Sales
3             Sudhir          Sales
4             Roj             Development

输出应为

EmployeeID    EmployeeName    Department
1             John            Sales
2             Robert          
3             Sudhir          
4             Roj             Development

阅读 187

收藏
2021-03-17

共1个答案

小编典典

你可以这样做:

;WITH CTE
AS
( 
   SELECT EmployeeID, EmployeeName, Department,
     ROW_NUMBER() OVER(PARTITION BY Department ORDER BY(SELECT 1)) rownum
  FROM table1
)
SELECT
  EmployeeId,
  EmployeeName,
  CASE rownum
    WHEN 1 THEN Department
    ELSE ''
  END AS Department
FROM CTE
ORDER BY EmployeeID;
2021-03-17