select ima01 as 料號,ima53 as 最近採購價,ima91 as 採購平均價,
ima531 as 市價,pmj05 as 幣別,pmj07 as 核價
from ima_file left outer join
(pmj_file a left outer join pmi_file b
on a.pmj01 = b.pmi01 and b.pmiconf = 'Y'
and a.pmj09 = (select max(pmj09) from pmj_file c,pmi_file d
where c.pmj01 = d.pmi01 and d.pmiconf = 'Y'
and a.pmj03 = c.pmj03))
on ima01 = a.pmj03 and (a.pmj07 != 0 or a.pmj07 is not null)
where imaacti = 'Y'
and ima01 like '02-SGM85-811';
=====================================================================
以上是我要抓料號02-SGM85-811的最近採購價/採購平均單價/市價/核價單價(最新的)
的相關欄位,但是因為該料號有二筆核價單,但我卻只要抓最近的核價單價,結果卻抓出二筆,
之所以要用outer join,是因為料件基本資料檔已有最近採購價/採購平均價/市價,但User還要同時看到該料號核價單價,而核價單價存放在pmj_file,因此用以上的SQL,但就是沒辦法只抓出一筆(含最新核價),請網友幫忙謝謝.
ima531 as 市價,pmj05 as 幣別,pmj07 as 核價
from ima_file left outer join
(pmj_file a left outer join pmi_file b
on a.pmj01 = b.pmi01 and b.pmiconf = 'Y'
and a.pmj09 = (select max(pmj09) from pmj_file c,pmi_file d
where c.pmj01 = d.pmi01 and d.pmiconf = 'Y'
and a.pmj03 = c.pmj03))
on ima01 = a.pmj03 and (a.pmj07 != 0 or a.pmj07 is not null)
where imaacti = 'Y'
and ima01 like '02-SGM85-811';
=====================================================================
以上是我要抓料號02-SGM85-811的最近採購價/採購平均單價/市價/核價單價(最新的)
的相關欄位,但是因為該料號有二筆核價單,但我卻只要抓最近的核價單價,結果卻抓出二筆,
之所以要用outer join,是因為料件基本資料檔已有最近採購價/採購平均價/市價,但User還要同時看到該料號核價單價,而核價單價存放在pmj_file,因此用以上的SQL,但就是沒辦法只抓出一筆(含最新核價),請網友幫忙謝謝.