我正在从旧查询创建一个新查询,我在结果中得到重复的行。我完全不知道为什么会这样?
以下是不产生重复项的旧SQL代码:
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
以下是创建重复项的较新版本:
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
希望有人能指出我正确的方向!
答案 0 :(得分:3)
我认为任何人都不可能为你解决这个问题。
也许这可以是一个开始:
用SELECT
替换新COUNT(*)
中的列,然后开始注释掉JOIN
,直到返回的计数发生变化。检查您刚注释掉的JOIN
是否导致了您不期望的重复行,并确保JOIN
的条件正确。
答案 1 :(得分:1)
LEFT / INNER JOIN与RIGHT JOIN混合 - 除非这些表具有一对一关系,否则您将有重复
您的“战斗计划”将如下:
您需要为所有涉及的表及其关系(1对多等)绘制一个模式,并确定哪个表是您的主要来源,哪些表是'查找'表。< / p>
首先删除所有查找表(并将其字段包含在结果数据集中),然后检查重复项
开始逐个添加查找表,检查每个重复项。
“TOP 100 PERCENT”实际上也是所有结果数据集。