left join 取一條數據


產品生產過程中要記錄產品在制作過程中的不良歷史,即使在一個工序,也可以產生多條不良歷史記錄,所有的不良信息保存在B表中。假設產品在打包工序,存放打包工序成品參數記錄的表A,數據是唯一的。但在打包過程中,產生的不良信息,可以有多條。然而,產品在當天的工序報表中,產品的最終不良信息,只是取最終的一次記錄為結果。使用關聯查詢A表,帶出的B表信息,只需要最后一條。

1.先從B表中只獲取最新的結果集

select * from AkBadInput a where Id=(select MAX(Id) from AkBadInput b where b.RepairKey=a.RepairKey and b.Barcode=a.Barcode and b.Process=a.Process)

2.通過關聯上面的結果集,獲取最新唯一的關聯結果

select AkFqc.*,c.JudgeResult
from AkFqc left join 
(select * from AkBadInput a where Id=(select MAX(Id) from AkBadInput b where b.RepairKey=a.RepairKey and b.Barcode=a.Barcode and b.Process=a.Process)) c
on AkFqc.Id=c.RepairKey and AkFqc.BarCode=c.Barcode and c.Process='FQC'

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM