------------- ------------------ ---------- --------------- --------------
|Id |EmployeePositionId|Date |Time |TypeEmployment|
------------- ------------------ ---------- --------------- --------------
|4399 |4557 |2022-01-10|60 |0 |
|4399 |4557 |2022-01-10|480 |0 |
|4399 |4561 |2022-01-10|540 |1 |
|4399 |4559 |2022-01-10|540 |2 |
|2448 |3017 |2022-01-31|480 |0 |
|2448 |3017 |2022-01-28|480 |0 |
|3406 |3841 |2022-01-31|480 |1 |
|3406 |3841 |2022-01-28|480 |1 |
|3406 |3841 |2022-01-27|480 |1 |
|3406 |3841 |2022-01-26|480 |1 |
------------- ------------------ ---------- --------------- --------------
需要過濾記錄,使員工大多只有就業型別為 0 的職位。
我不能這樣做where TypeEmployment = 0,因為它會切斷具有私人面部 id 3406 的員工
但是我想見他,因為他沒有0型的位置
但是,PrivateFaceId 4399 的員工有三個職位:4557、4561、4559(分別為型別 0、1、2),我想過濾掉那些與型別 0 不匹配的條目
這類似于行的合并,而不是列。它采用型別 0 的第一個位置,如果不是,則使用型別 1,如果不是,則使用型別 2
最后,我想看看:
------------- ------------------ ---------- --------------- --------------
|Id |EmployeePositionId|Date |Time |TypeEmployment|
------------- ------------------ ---------- --------------- --------------
|4399 |4557 |2022-01-10|60 |0 |
|4399 |4557 |2022-01-10|480 |0 |
|2448 |3017 |2022-01-31|480 |0 |
|2448 |3017 |2022-01-28|480 |0 |
|3406 |3841 |2022-01-31|480 |1 |
|3406 |3841 |2022-01-28|480 |1 |
|3406 |3841 |2022-01-27|480 |1 |
|3406 |3841 |2022-01-26|480 |1 |
------------- ------------------ ---------- --------------- --------------
感謝您的任何幫助
uj5u.com熱心網友回復:
id內按需下單,取第一個
select *
from (
select *
, row_number() over(partition by Id order by TypeEmployment) rn
from mytable
) t
where rn=1
如果需要具有相同頂部的所有位置,則使用dense_rank()代替。row_number()TypeEmployment
uj5u.com熱心網友回復:
兩種解決方案。
第一個使用 a group by:
select * from Table1 where
("Id", "TypeEmployment") in
(select
"Id", min("TypeEmployment")
from Table1
group by "Id")
order by 5, 2 desc, 4, 3 desc
;
第二個使用視窗函式min:
WITH subq as (select
*, min("TypeEmployment") over (partition by "Id") mini
from Table1)
select
"Id",
"EmployeePositionId",
"Date",
"Time",
"TypeEmployment"
from
subq
where
"TypeEmployment"="mini"
order by 5, 2 desc, 4, 3 desc
;
DDL:
CREATE TABLE Table1
("Id" int, "EmployeePositionId" int, "Date" timestamp, "Time" int, "TypeEmployment" int)
;
INSERT INTO Table1
("Id", "EmployeePositionId", "Date", "Time", "TypeEmployment")
VALUES
(4399, 4557, '2022-01-10 00:00:00', 60, 0),
(4399, 4557, '2022-01-10 00:00:00', 480, 0),
(4399, 4561, '2022-01-10 00:00:00', 540, 1),
(4399, 4559, '2022-01-10 00:00:00', 540, 2),
(2448, 3017, '2022-01-31 00:00:00', 480, 0),
(2448, 3017, '2022-01-28 00:00:00', 480, 0),
(3406, 3841, '2022-01-31 00:00:00', 480, 1),
(3406, 3841, '2022-01-28 00:00:00', 480, 1),
(3406, 3841, '2022-01-27 00:00:00', 480, 1),
(3406, 3841, '2022-01-26 00:00:00', 480, 1)
;
輸出:
| ID | 員工職位 ID | 日期 | 時間 | 型別就業 |
|---|---|---|---|---|
| 4399 | 4557 | 2022-01-10T00:00:00Z | 60 | 0 |
| 4399 | 4557 | 2022-01-10T00:00:00Z | 480 | 0 |
| 2448 | 3017 | 2022-01-31T00:00:00Z | 480 | 0 |
| 2448 | 3017 | 2022-01-28T00:00:00Z | 480 | 0 |
| 3406 | 3841 | 2022-01-31T00:00:00Z | 480 | 1 |
| 3406 | 3841 | 2022-01-28T00:00:00Z | 480 | 1 |
| 3406 | 3841 | 2022-01-27T00:00:00Z | 480 | 1 |
| 3406 | 3841 | 2022-01-26T00:00:00Z | 480 | 1 |
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/418794.html
標籤:
上一篇:找到另一個表中的匹配項時更新列
