Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

getting duplicate rows when adding a new join Expand / Collapse
Author
Message
Posted Wednesday, July 31, 2013 11:39 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, July 31, 2013 2:18 PM
Points: 1, Visits: 10
I am working on some Else's code, I am enhancing a report

the program the code is coming from is visual foxpro as pass through SQL (same as sql server query code)
the code is being sent to sql server 2005 through foxpro's sqlexec command

I have been working on this for 3 days, I am on a contract and need to resolve this, please help

Thanks,


Michael

*********************************************************************8
the SQL server tables are -

aritem - count: 79018, match fields - fcustno, fpartno, substring(fsokey,1,6), frev, fcinvoice
slcdpm - count: 909, match fields - fcustno
sorels - count:62080 match fields - fsono
inmast - count:9505 match fields - fpartno, frev
armast - count: 54191 match fields - fcinvoive

sql code below
*******************************************************
use m2mdata01
go
select aritem.fcustno,
slcdpm.fcompany,
slcdpm.fcity,
slcdpm.fstate,
slcdpm.fcountry,
aritem.fcinvoice,
aritem.fitem,
aritem.fpartno,
aritem.frev,
aritem.fmdescript,
armast.finvdate,
sorels.fmatlcost,
sorels.flabcost,
sorels.fovhdcost,
sorels.fsono,
CASE WHEN aritem.fprodcl IS NULL
THEN ''
ELSE aritem.fprodcl
END AS fprodcl,
CASE WHEN inmast.fgroup IS NULL
THEN ''
ELSE inmast.fgroup
END AS fgroup,
aritem.fshipqty,
aritem.fprice,
aritem.ftotprice,
aritem.fcost AS INV_cost,
CASE WHEN inmast.fstdcost IS NULL
THEN .00000
WHEN SUBSTRING(aritem.fcinvoice,1,2) = 'CM'
AND armast.fcsource NOT IN ('', 'R')
THEN .00000
ELSE inmast.fstdcost
END AS fstdcost,
CASE WHEN inmast.fstdcost IS NULL
THEN .00000
WHEN SUBSTRING(aritem.fcinvoice,1,2) = 'CM'
AND armast.fcsource NOT IN ('', 'R')
THEN .00000
ELSE inmast.fstdcost * aritem.fshipqty
END AS STD_T_COST,
CASE WHEN inmast.flastcost IS NULL
THEN .00000
WHEN SUBSTRING(aritem.fcinvoice,1,2) = 'CM'
AND armast.fcsource NOT IN ('', 'R')
THEN .00000
ELSE inmast.flastcost
END AS flastcost,
CASE WHEN inmast.flastcost IS NULL
THEN .00000
WHEN SUBSTRING(aritem.fcinvoice,1,2) = 'CM'
AND armast.fcsource NOT IN ('', 'R')
THEN .00000
ELSE inmast.flastcost * aritem.fshipqty
END AS LST_T_COST,
CASE WHEN slcdpm.fcountry IN ('United States')
THEN '1'
ELSE '2'
END as intl,
CASE WHEN SUBSTRING(aritem.fsalesacc,1,1) = '4'
THEN '1'
ELSE '2'
END as nonsales,
CASE WHEN aritem.fcustno IN ('010134', '010164', '011202', '010011')
THEN '1'
ELSE '2'
END AS interco,
aritem.fsalesacc
FROM aritem LEFT OUTER JOIN slcdpm
ON aritem.fcustno = slcdpm.fcustno
***********************************************************************
when I add this code to the select it changes the results record count from 198 to 3075
the correct count is 198
************************************************************************
left outer join sorels
on sorels.fsono = substring(aritem.fsokey,1,6)


end of added code
*************************************************************************
LEFT OUTER JOIN inmast
ON aritem.fpartno = inmast.fpartno
AND aritem.frev = inmast.frev
LEFT OUTER JOIN armast
ON aritem.fcinvoice = armast.fcinvoice
WHERE armast.fdgldate >= '06/01/2013'
AND armast.fdgldate <= '06/30/2013'
AND armast.fcstatus <> 'V'
AND ((SUBSTRING(aritem.fcinvoice,1,2) <> 'CM' AND armast.fcsource = 'S')
OR (SUBSTRING(aritem.fcinvoice,1,2) = 'CM' AND armast.fcsource IN ('R', 'C', ' ', 'I')))
AND 1=1
AND 1=1
AND 1=1

order by aritem.fcustno, aritem.fcinvoice, aritem.fsokey
Post #1479619
Posted Wednesday, July 31, 2013 11:49 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 5:01 PM
Points: 3,110, Visits: 7,844
If you're getting more rows than you should after adding a join, it's probably because you should be joining to that table using more than just 1 join condition. We can't say for sure what the problem is without knowing more about the data and tables.



Alvin Ramard
Memphis PASS Chapter

All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.
Post #1479625
Posted Wednesday, July 31, 2013 12:56 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, January 28, 2014 8:15 AM
Points: 3,068, Visits: 4,639
what happens if you replace "left outer join sorels" with an inner join?

_____________________________________
Pablo (Paul) Berzukov

Author of Understanding Database Administration available at Amazon and other bookstores.

Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
Post #1479652
Posted Wednesday, July 31, 2013 12:58 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, July 31, 2013 2:18 PM
Points: 1, Visits: 10
same results, tried it
Post #1479656
Posted Wednesday, July 31, 2013 1:07 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, January 28, 2014 8:15 AM
Points: 3,068, Visits: 4,639
mcummings (7/31/2013)
same results, tried it


I would check the data - visually validate if returned rows are the correct ones(*) or not, if not, check what is the query returning.

(*) correct ones as defined on the actual predicate, not on the business specification.


_____________________________________
Pablo (Paul) Berzukov

Author of Understanding Database Administration available at Amazon and other bookstores.

Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
Post #1479658
Posted Wednesday, July 31, 2013 1:08 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 4:45 PM
Points: 1,786, Visits: 5,681
You haven't supplied enough information, but you just need to figure out the correct join.

My guess is that because sorels has costs in it that they might relate to items and you might need to also join on

aritem.fitem
or maybe
aritem.fpartno

But we don't know the structure of the tables, so can only guess


MM


  • MMGrid Addin
  • MMNose Addin


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Post #1479659
    Posted Wednesday, July 31, 2013 1:14 PM
    Forum Newbie

    Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

    Group: General Forum Members
    Last Login: Wednesday, July 31, 2013 2:18 PM
    Points: 1, Visits: 10
    adding sorels.fpartno = aritem.fpart cuts it down from 3075 to 1204
    but there's no fitem in sorels
    Post #1479660
    Posted Wednesday, July 31, 2013 1:15 PM
    Forum Newbie

    Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

    Group: General Forum Members
    Last Login: Wednesday, July 31, 2013 2:18 PM
    Points: 1, Visits: 10
    data looks ok, it lists the same record 3 times
    Post #1479661
    Posted Wednesday, July 31, 2013 1:15 PM


    SSC-Insane

    SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

    Group: General Forum Members
    Last Login: Yesterday @ 9:59 PM
    Points: 21,297, Visits: 14,988
    With that change do you now still see duplicate results?



    Jason AKA CirqueDeSQLeil
    I have given a name to my pain...
    MCM SQL Server


    SQL RNNR

    Posting Performance Based Questions - Gail Shaw
    Posting Data Etiquette - Jeff Moden
    Hidden RBAR - Jeff Moden
    VLFs and the Tran Log - Kimberly Tripp
    Post #1479662
    Posted Wednesday, July 31, 2013 1:18 PM


    SSCommitted

    SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

    Group: General Forum Members
    Last Login: Yesterday @ 4:45 PM
    Points: 1,786, Visits: 5,681
    mcummings (7/31/2013)
    adding sorels.fpartno = aritem.fpart cuts it down from 3075 to 1204
    but there's no fitem in sorels


    As I said, WE are guessing - you shouldn't be .

    If you don't understand how those tables are related, you should not be changing anything until you learnt the relationships.

    Sorry to be blunt, but if you are being paid to do this, you should learn not guess.


    MM


  • MMGrid Addin
  • MMNose Addin


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Post #1479663
    « Prev Topic | Next Topic »

    Add to briefcase 12»»

    Permissions Expand / Collapse