我有一個表格,如下:
| Order_ID | 船舶編號 | 專案代碼 數量 已采摘的數量。采摘日期 |
|---|---|---|
| 1111 | 11 |
從這個表中。 我只想顯示有最新的ship_num資訊的行,而不是最新的pick_date資訊(我被引導到一個類似的問題,需要回傳有最新進入時間的行,我不是在尋找這個),對于一個訂單,即,我希望它如下
。我試著做了以下的查詢,
select order_id, max(ship_num), item_code, qty_to_pick, qty_picked, pick_date
from table1
group by order_id, item_code, qty_to_pick, qty_picked, pick_date
如果有任何幫助,我們將不勝感激。
預先感謝。
uj5u.com熱心網友回復:
使用max(ship_num)是一個好主意,但你應該使用分析版本(有一個OVER子句)。
select *
from
(
select t. *, max(ship_num) over (partition by orders_id) as orders_max_ship_num
from table1 t1
) with_max
where ship_num = orders_max_ship_num
order by order_id, item_code;
uj5u.com熱心網友回復:
你可以使用DENSE_RANK()得到這個結果。
查詢
; with cte as (
select rnk = dense_rank( )
over (Partition by order_id order by ship_num desc)
,*
from table_name
)
選擇*
from cte
Where rnk =1;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/320178.html
標籤:
上一篇:連接兩個表,同時包括空號
