我有如下表。通常它有更多的列,但為了簡單起見,我在這裡附上了最重要的部分。具有相同的行ItemId
幾乎相同。有時ts
標誌等於0
或不同1
。一個 並不總是有兩行ItemId
,但它們總是按如下順序排列 ( flag asc
)。我想總是只獲取 1 行,但如果有兩個標誌,我想用Flag=0
.
從:
物品編號 | 旗幟 | ts |
---|---|---|
X | 0 | 2021-01-01 02:00 |
X | 1 | 2021-01-01 03:00 |
和 | 0 | 2021-01-01 03:00 |
和 | 1 | 2021-01-01 02:00 |
和 | 1 | 2021-01-01 01:00 |
瓦 | 1 | 2021-01-01 01:00 |
到:
ItemId|ts
------|-----------------
x |2021-01-01 02:00
y |2021-01-01 03:00
z |2021-01-01 01:00
w |2021-01-01 01:00
我試過MAX
,但它不能使用,因為 ts 沒有邏輯,所以日期是錯誤的。
您可以使用row_number()
:
select *
from(select t.*, row_number() over(partition by itemid order by flag) as seq
from table t
) t
where seq = 1;
Dieser Artikel stammt aus dem Internet. Bitte geben Sie beim Nachdruck die Quelle an.
Bei Verstößen wenden Sie sich bitte [email protected] Löschen.
Lass mich ein paar Worte sagen