tags:

views:

75

answers:

2

I am creating a new query from an old query and I'm getting duplicate rows in my result. I have absolutely no idea why this is happening?

Here is the old SQL codes which does not produce duplicates:

XtraViwStock

SELECT     TOP 100 PERCENT dbo.qexp_detail.in_date AS inDate, RTRIM(dbo.qexp_detail.stock_locn) AS stockLocn, { fn LCASE(RTRIM(dbo.qexp_detail.in_locn)) 
                      } AS inLocn, dbo.qin_doc.in_doc_no AS inDoc, RTRIM(dbo.qexp_detail.client_plt_id) AS pltId, dbo.xtraviwItem.cm, dbo.xtraviwItem.vari, 
                      dbo.xtraviwItem.pack, dbo.qexp_detail.prod_grp AS pGrp, dbo.qexp_detail.prod_char AS pChar, dbo.xtraviwItem.grd, dbo.xtraviwItem.sizeCount, 
                      dbo.xtraviwItem.brand, dbo.xtratblNotifPltId.chrNotifPltId AS notifPltId, dbo.xtraviwItem.ic, dbo.qexp_detail.targ_mkt AS tm, dbo.qexp_detail.farm AS puc,
                       dbo.qexp_detail.ctn_qty AS ctns, CAST(dbo.qexp_detail.ctn_qty AS float) / CAST(dbo.xtraviwCtnsPerPlt_st.cpp AS float) AS plts, 
                      dbo.xtraviwCtnsPerPlt_st.cpp, dbo.qexp_detail.plt_base AS base, dbo.xtraviwMessage.msgText AS inDocMsg, dbo.xtratblSpec.intSpecId AS spec1Id, 
                      dbo.xtratblSpec.strInstruction AS spec1Instruct, dbo.xtratblSpec.dtmSpecDate AS spec1SpecDate, 
                      dbo.xtratblSpecPallet.dtmLoadDate AS spec1LoadDate, dbo.xtratblSpecPallet.strLoadLocn AS spec1LoadLocn, 
                      dbo.xtratblSpecPallet.strReceiver AS spec1Rec, dbo.xtratblSpecPallet.strPOD AS spec1Pod, dbo.xtratblSpec2.intSpecId AS spec2Id, 
                      dbo.xtratblSpec2.strInstruction AS spec2Instruct, dbo.xtratblSpec2.dtmSpecDate AS spec2SpecDate, 
                      dbo.xtratblSpecPallet2.dtmLoadDate AS spec2LoadDate, dbo.xtratblSpecPallet2.strLoadLocn AS spec2LoadLocn, 
                      dbo.xtratblSpecPallet2.strReceiver AS spec2Rec, dbo.xtratblSpecPallet2.strPOD AS spec2Pod, dbo.xtraviwItem.org, dbo.qexp_detail.tran_user, 
                      dbo.qexp_detail.tran_datetime, 
                      dbo.xtratblSupplier.strProperName + ' ' + dbo.xtratblSupplier.strProperSurname + ', ' + dbo.xtratblSupplier.strProperFarms + ', ' + dbo.qexp_detail.supplier
                       AS producer, { fn LCASE(RTRIM(dbo.qin_doc.farm)) } AS packLocn, dbo.xtraviwItem.varietyGroup, dbo.xtraviwItem.brandDesc, dbo.xtraviwItem.kg, 
                      dbo.qexp_detail.client_plt_type AS pltHt, dbo.xtraviwItem.hcHt, dbo.xtraviwItem.stdHt, dbo.qexp_detail.build_flag AS buildFlag
FROM         dbo.xtraviwCurrentSeason INNER JOIN
                      dbo.qexp_detail ON dbo.xtraviwCurrentSeason.season = dbo.qexp_detail.season LEFT OUTER JOIN
                      dbo.qsupplier LEFT OUTER JOIN
                      dbo.xtratblSupplier ON dbo.qsupplier.supplier = dbo.xtratblSupplier.chrSupplier ON dbo.qexp_detail.supplier = dbo.qsupplier.supplier LEFT OUTER JOIN
                      dbo.xtratblSpec2 RIGHT OUTER JOIN
                      dbo.xtratblSpecPallet2 ON dbo.xtratblSpec2.intSpecId = dbo.xtratblSpecPallet2.intSpecId ON 
                      dbo.qexp_detail.client_plt_id = dbo.xtratblSpecPallet2.chrPltId AND dbo.qexp_detail.season = dbo.xtratblSpecPallet2.intSeason LEFT OUTER JOIN
                      dbo.xtratblSpec RIGHT OUTER JOIN
                      dbo.xtratblSpecPallet ON dbo.xtratblSpec.intSpecId = dbo.xtratblSpecPallet.intSpecId ON 
                      dbo.qexp_detail.client_plt_id = dbo.xtratblSpecPallet.chrPltId AND dbo.qexp_detail.season = dbo.xtratblSpecPallet.intSeason LEFT OUTER JOIN
                      dbo.qfarm ON dbo.qexp_detail.farm = dbo.qfarm.farm LEFT OUTER JOIN
                      dbo.xtraviwMessage RIGHT OUTER JOIN
                      dbo.qin_doc ON dbo.xtraviwMessage.msgNr = dbo.qin_doc.mesg_no ON dbo.qexp_detail.in_load_id = dbo.qin_doc.in_load_id LEFT OUTER JOIN
                      dbo.xtraviwItem ON dbo.qexp_detail.item_code = dbo.xtraviwItem.item LEFT OUTER JOIN
                      dbo.xtratblNotifPltId ON dbo.qexp_detail.season = dbo.xtratblNotifPltId.intSeason AND 
                      dbo.qexp_detail.client_plt_id = dbo.xtratblNotifPltId.chrDiparPltId LEFT OUTER JOIN
                      dbo.xtraviwCtnsPerPlt_st ON dbo.qexp_detail.season = dbo.xtraviwCtnsPerPlt_st.cppSeason AND 
                      dbo.qexp_detail.season_key = dbo.xtraviwCtnsPerPlt_st.cppCm AND dbo.qexp_detail.client_plt_id = dbo.xtraviwCtnsPerPlt_st.cppPltId
WHERE     (dbo.qexp_detail.plt_status = 'S')
ORDER BY dbo.xtraviwItem.cm, dbo.qexp_detail.in_date, RTRIM(dbo.qexp_detail.stock_locn), RTRIM(dbo.qexp_detail.client_plt_id), dbo.qexp_detail.farm

And here is the newer version that creates duplicates:

AllStock2010

SELECT     TOP 100 PERCENT dbo.qexp_detail.in_date AS InDate, dbo.qexp_detail.stock_locn AS StockLocn, dbo.qexp_detail.in_locn AS InLocn, 
                      dbo.qin_doc.in_doc_no AS InDoc, dbo.qxfer_detail.instruction_no AS TransferInstructNo, dbo.qexp_detail.instruction_no AS LoadInstructNo, 
                      dbo.qexp_detail.client_plt_id AS PltID, dbo.xtraviwItem.cm AS CM, dbo.xtraviwItem.vari AS Variety, dbo.xtraviwItem.pack AS Pack, 
                      dbo.qexp_detail.prod_grp AS ProdGrp, dbo.qexp_detail.prod_char AS ProdChar, dbo.xtraviwItem.grd AS Grade, 
                      dbo.xtraviwItem.sizeCount AS SizeCount, dbo.xtraviwItem.brand AS Brand, dbo.xtraviwItem.ic AS IC, dbo.qexp_detail.targ_mkt AS TM, 
                      dbo.qexp_detail.farm AS PUC, dbo.qexp_detail.ctn_qty AS Cartons, CAST(dbo.qexp_detail.ctn_qty AS float) / CAST(dbo.xtraviwCtnsPerPlt_st.cpp AS float)
                       AS Plts, dbo.xtraviwCtnsPerPlt_st.cpp AS CpP, dbo.qexp_detail.plt_base AS PltBase, dbo.xtraviwMessage.msgText AS InDocMsg, 
                      dbo.xtraviwItem.org AS Org, dbo.qexp_detail.tran_user, dbo.qexp_detail.tran_datetime, dbo.qexp_detail.supplier AS Producer, 
                      dbo.qin_doc.farm AS PackLocn, dbo.qin_detail.pack_date AS PackDate, dbo.xtraviwItem.varietyGroup AS VarietyGroup, 
                      dbo.xtraviwItem.brandDesc AS BrandDesc, dbo.xtraviwItem.kg AS Kg, dbo.qexp_detail.client_plt_type AS PltHt, 
                      dbo.qexp_detail.build_flag AS BuildFlag, dbo.xtraviwItem.hcHt AS HcHt, dbo.xtraviwItem.stdHt AS StdHt, dbo.qexp_detail.plt_status AS PltStatus
FROM         dbo.xtraviwItem RIGHT OUTER JOIN
                      mynhardt.CurrentSeason INNER JOIN
                      dbo.qexp_detail ON mynhardt.CurrentSeason.Season = dbo.qexp_detail.season ON 
                      dbo.xtraviwItem.item = dbo.qexp_detail.item_code LEFT OUTER JOIN
                      dbo.qxfer_detail ON dbo.qexp_detail.client_plt_id = dbo.qxfer_detail.client_plt_id LEFT OUTER JOIN
                      dbo.qin_detail ON dbo.qexp_detail.client_plt_id = dbo.qin_detail.client_plt_id LEFT OUTER JOIN
                      dbo.xtraviwCtnsPerPlt_st ON dbo.qexp_detail.season = dbo.xtraviwCtnsPerPlt_st.cppSeason AND 
                      dbo.qexp_detail.season_key = dbo.xtraviwCtnsPerPlt_st.cppCm AND 
                      dbo.qexp_detail.client_plt_id = dbo.xtraviwCtnsPerPlt_st.cppPltId LEFT OUTER JOIN
                      dbo.qin_doc ON dbo.qexp_detail.in_load_id = dbo.qin_doc.in_load_id LEFT OUTER JOIN
                      dbo.xtraviwMessage ON dbo.qin_doc.mesg_no = dbo.xtraviwMessage.msgNr
WHERE     (dbo.qexp_detail.plt_status = 'S')
ORDER BY dbo.xtraviwItem.cm, dbo.qexp_detail.in_date, dbo.qexp_detail.stock_locn, dbo.qexp_detail.client_plt_id, dbo.qexp_detail.farm

Hopefully someone can point me in the right direction!

+3  A: 

I don't think it's possible for anyone to solve that for you.

Maybe this can be a start:
Replace the columns in your new SELECT by a COUNT(*) and start commenting out JOINs until the returned count changes. Check if the JOIN you just commented out caused that duplicate rows you did not expect and make sure that the conditions for that JOIN are right.

Peter Lang
+1  A: 

LEFT/INNER JOINs are mixed with RIGHT JOINs - unless those tables have one-to-one relationship, you will have duplicates

Your 'battle plan' would be the following:

  1. You need to draw a schema for all involved tables and their relationship (1-to-many, etc.) and determine which table(s) is your main source and which tables are 'lookup' tables.

  2. Remove all lookup tables first (and their fields included into resulting dataset) and then check for duplicates

  3. Start adding lookup tables one by one checking each for duplicates.

Also "TOP 100 PERCENT" is really all resulting dataset.

seletit