SQL server min和MAX日期

时间:2018-06-01 11:18:45

标签: sql sql-server tsql

我有一张包含以下数据的表格

enter image description here

我希望输出为

enter image description here

EMP 25FC0938-88EE有3个条目。应在A列中输入最早日期的ID,并在B列中休息。

我如何在SQL

中实现这一目标

1 个答案:

答案 0 :(得分:1)

您可以使用first_value()

select firstvalue as columnA, id as columnB
from (select *, first_value(id) over (partition by us order by date) as firstvalue
      from table
     ) t
where id <> firstvalue;

如果要插入前一个查询的结果集,请使用INSERT . . INTO语句

insert into table (columnA, columnB)
select firstvalue as columnA, id as columnB
from (select *, first_value(id) over (partition by us order by date) as firstvalue
      from table
     ) t
where id <> firstvalue;