因为我不是编写SQL查询的专家所以想要帮助。
我有以下给定的数据集。
我需要编写一个查询来查找每个工单的"最后关闭的seq_id"
最终查询应仅返回seq_id = 24,28,32的行 enter image description here
答案 0 :(得分:2)
试试这个:
SELECT sts
,workorder_id
,MAX(SEQ_ID) AS Last_Seq_ID
FROM mytablename
WHERE sts = 'Closed' -- Only include Closed records
GROUP BY sts, workorder_id
ORDER BY workorder_id -- This line is optional; it will sort your result
答案 1 :(得分:1)
这将起作用
SELECT workorder_id
,max(seq_id)
from your_table
where sts="Closed"
group by workorder_id
答案 2 :(得分:0)
假设你的意思是“32”而不是“31”,那么row_number()
就是典型的方法:
select t.*
from (select t.*,
row_number() over (partition by workorder_id order by seq_id desc) as seqnum
from t
where sts = 'Closed'
) t
where seqnum = 1;
答案 3 :(得分:0)
试试这个:
select workorder_id,max(seq_id) as seqid
from testtable
where sts='Closed'
group by workorder_id