如何只为MSSQL中的每个重复行获取一条记录

时间:2017-10-16 10:23:12

标签: sql-server

如何以最低的时间值提取多行中的第一条记录。

类似的东西:

PersNr |  Name |    Day   | From |   To   | TaskNr 
------------------------------------------------
1000     Peter  2017-10-16  25050  -2        110
1001     Emily  2017-10-16  25038  -2        110
1010     Greg   2017-10-16  28306  31371     120
1010     Greg   2017-10-16  31371  -2        150
1012     Jon    2017-10-16  24805  26716     170
1012     Jon    2017-10-16  26716  29227     110
1012     Jon    2017-10-16  29227   -2       150

我需要唯一的名字,他们什么时候开始工作(从价值最低)。

它应该是这样的:

PersNr |  Name |    Day   | From |   To   | TaskNr 
------------------------------------------------
1000     Peter  2017-10-16  25050  -2        110
1001     Emily  2017-10-16  25038  -2        110
1010     Greg   2017-10-16  28306  31371     120
1012     Jon    2017-10-16  24805  26716     170

我创建了这样的表:

SELECT AB.PersNr
              ,P.Name AS name
              ,CONVERT(char(10),DATEADD(DAY, AB.Tag, '30.12.1899'),126) AS Day
              ,AB.From
              ,AB.To
              ,AB.TaskNr
              FROM AStpFromTo AB
                LEFT JOIN Tasks A ON (A.TaskNr = AB.TaskNr)
                INNER JOIN Person P ON (P.PersNr = AB.PersNr)
              WHERE P.Department = 170 AND AB.Day = DATEDIFF(DAY, '30.12.1899', GETDATE())

3 个答案:

答案 0 :(得分:0)

尝试使用此查询。

;with cte AS (
select *,ROW_NUMBER()over(partition by PersNr order by (select 1) )rnum from tab
)
select * from cte where rnum=1

答案 1 :(得分:0)

您只需要按任务顺序排序功能,以获得最低的功能:

WITH CTE
AS
(
   SELECT AB.PersNr
     ,P.Name AS name
     ,CONVERT(char(10),DATEADD(DAY, AB.Tag, '30.12.1899'),126) AS Day
     ,AB.From
     ,AB.To
     ,AB.TaskNr
     , ROW_NUMBER() OVER(PARTITION BY PerNr ORDER BY [From]) AS RN
   FROM AStpFromTo AB
   LEFT JOIN Tasks A ON (A.TaskNr = AB.TaskNr)
   INNER JOIN Person P ON (P.PersNr = AB.PersNr)
   WHERE P.Department = 170 AND AB.Day = DATEDIFF(DAY, '30.12.1899', GETDATE())
) 
SELECT * 
FROM CTE 
WHERE RN = 1;

结果:

| PersNr |  Name |                  Day |  From |    To | TaskNr | RN |
|--------|-------|----------------------|-------|-------|--------|----|
|   1000 | Peter | 2017-10-16T00:00:00Z | 25050 |    -2 |    110 |  1 |
|   1001 | Emily | 2017-10-16T00:00:00Z | 25038 |    -2 |    110 |  1 |
|   1010 |  Greg | 2017-10-16T00:00:00Z | 28306 | 31371 |    120 |  1 |
|   1012 |   Jon | 2017-10-16T00:00:00Z | 24805 | 26716 |    170 |  1 |

答案 2 :(得分:0)

尝试这种方法:

create table #tbl(PersNr int,Name varchar(100), Day date,"From" int,"To" int,TaskNr int) 

insert into #tbl values
(1000     ,'Peter'  ,'2017-10-16'  ,25050  ,-2        ,110),
(1001     ,'Emily'  ,'2017-10-16'  ,25038  ,-2        ,110),
(1010     ,'Greg'   ,'2017-10-16'  ,28306  ,31371     ,120),
(1010     ,'Greg'   ,'2017-10-16'  ,31371  ,-2        ,150),
(1012     ,'Jon'    ,'2017-10-16'  ,24805  ,26716     ,170),
(1012     ,'Jon'    ,'2017-10-16'  ,26716  ,29227     ,110),
(1012     ,'Jon'    ,'2017-10-16'  ,29227  , -2       ,150)

sql查询:

Select iq.* from
(
select a.*, ROW_NUMBER() OVER(PARTITION BY PersNr ORDER BY "From") AS RowNumber 
from #tbl a
)iq
where iq.RowNumber = 1

输出:

PersNr      Name      Day        From        To          TaskNr      RowNumber
----------- -------- ---------- ----------- ----------- ----------- -----------
1000        Peter    2017-10-16 25050       -2          110         1
1001        Emily    2017-10-16 25038       -2          110         1
1010        Greg     2017-10-16 28306       31371       120         1
1012        Jon      2017-10-16 24805       26716       170         1