我的SQL查询可以显示以下结果:
Name Project Date
Peter A 2014-12-11
Peter C 2014-12-12
Peter D 2014-12-13
John B 2014-12-12
我想显示如下结果。 (未显示相同的名称)
Name Project Date
Peter A 2014-12-11
C 2014-12-12
D 2014-12-13
John B 2014-12-12
我的数据库服务器是MS SQL Server 2012 Standard。如何通过SQL查询获得上述结果?
谢谢,
贝
答案 0 :(得分:1)
使用window Function
查找每个组中的第一个日期(名称)
SELECT CASE WHEN rn = 1 THEN Name ELSE '' END Name,
Project,
Dates
FROM (SELECT Row_number()OVER (partition BY Name ORDER BY Dates) rn,*
FROM Tablename) a
答案 1 :(得分:0)
您可以使用ROW_NUMBER()
功能
;WITH CTE AS
(
SELECT Name As OldName, Project, Date,
Row_Number() OVER (Partition By Name Order By Project) rn
From YourTable
)
SELECT CASE WHEN rn = 1 THEN OldName ELSE '' End Name, Project, Date
FROM CTE
ORDER BY OldName, rn
答案 2 :(得分:-1)
SELECT CASE WHEN ROW_NUMBER() OVER ( PARTITION BY Name ORDER BY Name ) = 1
THEN Name
ELSE ''
END AS Name ,
Project ,
Date
FROM t1