[英]Get the least closest value
我有以下數據
Docnum ItemCode opnum startdate result
73305 Wc-Trim 100 2017-08-18 2017-08-18
73305 WC-Asse 200 2017-11-29 2017-08-18
73305 Wc-id 300 2017-11-30 2017-11-29
73305 wc-qa 400 2017-12-01 2017-11-30
我需要獲得結果列中所示的輸出。 說明:1.Opnum 100是第一個操作,因此日期與開始日期相同。 2.對於opnum 200,它必須獲取opnum 100的開始日期。對於opnum 300,它必須獲取opnum 200的開始日期,依此類推。
注意:opnum並不總是100的倍數。對於我的查詢,我得到所有行的opnum 100的開始日期。
查詢:
select t0.DocNum,t1.itemcode,t1.U_SES_OP_NUM,t1.U_SES_DATESTART
,(select top 1 a0.U_SES_DATESTART from wor1 a0 where
t1.DocEntry = a0.DocEntry and a0.ItemType = 290 and
a0.U_SES_OP_NUM < t1.U_SES_OP_NUM)
from OWOR t0inner join WOR1 t1 on t0.DocEntry = t1.DocEntry and t1.ItemType = 290
where t0.docnum = 73305
order by t1.U_SES_OP_NUM
這通常會lag
。 由於MySQL不支持它,因此可以使用相關的子查詢來完成。
select t.*,
coalesce((select start_date from tbl
where docnum=t.docnum and opnum < t.opnum
order by opnum desc limit 1)
,start_date) as result
from tbl t
該解決方案假定每個opnum和docnum有一行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.