我有一张表,其中有EMPID列。在此表中,我有多个具有相同EMPID的行。现在我想获得最后一行不同的EmpID。请查看下表数据。我希望得到带有ID为3,5和7的Row的所有列。
我想要结果如下:
我尝试使用以下查询,但是无效。
SELECT ID,
EmpID,
Salary,
Leave,
TakenLeave,
field1,
field2,
field3
FROM Table_2
GROUP BY ID,
EmpID,
Salary,
Leave,
TakenLeave,
field1,
field2,
field3
答案 0 :(得分:4)
SELECT
TOP 1 WITH TIES
ID, EmpID, Salary, Leave, TakenLeave, field1, field2, field3
FROM [TABLE_NAME]
ORDER BY ROW_NUMBER() OVER(PARTITION BY EmpId ORDER BY Id DESC)
答案 1 :(得分:1)
按EmpId提供row_number分区,并按Id desc排序。
<强>查询强>
import { OnInit } from '@angular/core';
export class SomeClass implements OnInit {
ngOnInit() {
console.log("ngOnInit");
var x = document.getElementById("addToFav").innerText;
if (x=="Add to Favorites") {
document.getElementById("addToFav").innerText = "Remove from Favorites";
}
else {
document.getElementById("addToFav").innerText = "Add to Favorites";
}
}
}
答案 2 :(得分:1)
您可以使用此代码;
select ID, EmpID, Salary, Leave, TakenLeave, field1, field2, field3
from (select ID,
EmpID,
Salary,
Leave,
TakenLeave,
field1,
field2,
field3 row_number() over(partition by EmpID order by id desc) as que
from table_2)
where que = 1
答案 3 :(得分:0)
select * from myTable
where id in
(
select max(id) from myTable group by empId
);
Inner SQL选择每个empId的max(id),而外部select选择具有那些ID的行。