2014-09-04 29 views
0

這是查詢時,我運行它它給了我重複的記錄,但我刪除INNER JOIN planingOrderBookHeader PLOH ON sd.StyleID=PLOH.StyleIDPLOH.OderBookID列,它給記錄沒有重複,但我需要記錄與列名PLOH.OderBookID我該如何解決這個問題?雖然我在SQL Server 2008 R2中使用「Distinct」和「Group By」,但爲什麼使用內部連接但記錄重複?

SELECT 
     CPO.ID 
     ,CPO.StyleID 
     ,cpo.CustomerPo 
     ,PLOH.OderBookID 
     ,cpo.Process 
     ,CPD.Qty 
     ,cpd.RefID 
     ,cpd.Uom 
     ,UM.MeshType 
     ,PRC.Name 
     ,STC.NewStyleno 
     ,SD.TypeID 
     ,itm.Name Part 
     ,SD.ColorID 
     ,COL.Name Color 
     ,SD.FabricID 
     ,FAB.Name Faric 
     ,SHD.ItemID 
     ,ITD.Name Item 
     ,SHD.Image 
FROM CustomerPO_Header CPO 
    INNER JOIN styleconfirmHeader STC ON STC.StyleID=CPO.StyleID 
    INNER JOIN CustomerPo_Details CPD ON CPO.ID=CPD.CusPOHeaderID 
    INNER JOIN StyleHeader SHD ON SHD.StyleID=CPO.StyleID 
    INNER JOIN StyleDetails SD ON CPO.StyleID=SD.StyleID 
    INNER JOIN ItemDetails itm ON SD.TypeID=itm.ID 
    INNER JOIN ColorDetails COL ON SD.ColorID=COL.ID 
    INNER JOIN ItemFabricDetails FAB ON FAB.ID=SD.FabricID 
    INNER JOIN UOM UM ON Um.ID=CPD.Uom 
    INNER JOIN ItemTypeDetails ITD ON SHD.ItemID=ITD.ID 
    INNER JOIN process PRC ON PRC.ID=CPO.Process 
    INNER JOIN planingOrderBookHeader PLOH ON sd.StyleID=PLOH.StyleID 

PLZ幫我

回答

0

很難不知道你有什麼數據來檢驗。但是,您可以嘗試將導致您的重複項的表移動到子查詢中而不是聯接中。所以像這樣:

SELECT 
    CPO.ID 
    ,CPO.StyleID 
    ,cpo.CustomerPo 
    ,(SELECT PLOH.OderBookID FROM planingOrderBookHeader PLOH WHERE sd.StyleID=PLOH.StyleID) AS OderBookID 
    ,cpo.Process 
    ,CPD.Qty 
    ,cpd.RefID 
    ,cpd.Uom 
    ,UM.MeshType 
    ,PRC.Name 
    ,STC.NewStyleno 
    ,SD.TypeID 
    ,itm.Name Part 
    ,SD.ColorID 
    ,COL.Name Color 
    ,SD.FabricID 
    ,FAB.Name Faric 
    ,SHD.ItemID 
    ,ITD.Name Item 
    ,SHD.Image 
FROM CustomerPO_Header CPO 
INNER JOIN styleconfirmHeader STC ON STC.StyleID=CPO.StyleID 
INNER JOIN CustomerPo_Details CPD ON CPO.ID=CPD.CusPOHeaderID 
INNER JOIN StyleHeader SHD ON SHD.StyleID=CPO.StyleID 
INNER JOIN StyleDetails SD ON CPO.StyleID=SD.StyleID 
INNER JOIN ItemDetails itm ON SD.TypeID=itm.ID 
INNER JOIN ColorDetails COL ON SD.ColorID=COL.ID 
INNER JOIN ItemFabricDetails FAB ON FAB.ID=SD.FabricID 
INNER JOIN UOM UM ON Um.ID=CPD.Uom 
INNER JOIN ItemTypeDetails ITD ON SHD.ItemID=ITD.ID 
INNER JOIN process PRC ON PRC.ID=CPO.Process 

當然這可能會有性能影響。

+0

謝謝親愛的它不工作 – tharinda 2014-09-04 07:08:04

相關問題