1. 程式人生 > >inner join 資料重複問題

inner join 資料重複問題

 在查詢資料時. 寫了一段sql 很奇怪.查詢前5個表的資料時,不會出現重複資料

 直到關聯第六個表時(T_IM_InventoryBalance ),重複資料很多。一時間很鬱悶.隨後諮詢實施人員才知。

 原來T_IM_InventoryBalance 本身就有多條重複資料,也就是說當material.fid = ib.fmaterialId 關聯時

 是一對多的關係..需要設定多個條件才能避免重複.

 select material.fid from T_IM_StockTransferBill stb
 inner join T_IM_StockTransferBillEntry stbe on stb.fid= stbe.fparentId
 inner join t_bd_material material on material.fid = stbe.fmaterialId
 inner join T_BD_MaterialCompanyInfo mci on material.fid = mci.FMaterialID
 inner join T_DB_WAREHOUSE warehouse on stbe.FIssueWarehouseID = warehouse.fid
 inner join T_IM_InventoryBalance

ib on material.fid = ib.fmaterialId

 and ib.FWarehouseID = warehouse.fid and。。。。。
 inner join T_IM_STORESTATE storestate on ib.FStoreStatusID = storestate.fid
 inner join T_ORG_Storage storage on ib.FStorageOrgUnitID = storage.fid
 and fbizdate >=(select fbeginDate from T_BD_Period where fnumber ='200910')
 and fbizDate <=(select fendDate from T_BD_Period where fnumber ='200910')
 and warehouse.fnumber = 'K11'
 and stb.FBaseStatus = 7
 and ib.FYear = 2009 and ib.FPeriod = 10
 and stb.FIssueStorageOrgUnitID = '4ueEzZGDSYuVfAD/hLHijsznrtQ='
 and stb.FReceiptStorageOrgUnitID
 in ('eWbCBD/ZQ1+ebRrZDuxF3cznrtQ=','AcgiAdKIT7Cs4dP40U7KTcznrtQ=',
     'nXnw4GGNRh+AxZ/cMEMFpcznrtQ=','8vJD+kZuRLuQlkF2ysZ3b8znrtQ=',
     '7b2aymBESKGHPqv8xIKs3cznrtQ=')