SQL statement to return only one row when a related table has duplicate matching rows

Posted on 2007-10-19
Last Modified: 2012-06-27
A SQL statement is causing issues because one of the tables I join with has duplicate records, which is having the effect of duplicating some of the SUM fields.  How do I write an SQL which joins two tables such that only one occurrence of the target table is taken?  For example, inventory table is joined with problemtable.  Problemtable has 2 records that are duplicated and match the inventory.product.  This will result in two records in the result set.  How do I only return one row?
sele sum(inventory.qty), problemtable.product
where inventory.product = problemtable.product.
Question by:andremara
    LVL 8

    Expert Comment

    hey andremara,

    first, try to use "on" statement in order to specify your join criterias, this does the same thing but will have a better and the right syntax for the compiler and for a reader.

    join statements always return duplicate rows if a row is duplicated. you can use a temporary table or a sub query to use "group by" to eliminate your duplicate rows before you join your tables.

    good luck =)
    LVL 8

    Expert Comment

    eg. this will be a subquery or a temp table created with a query like "(select a,b,c from myProblemTable group by a,b,c)"
    LVL 8

    Expert Comment

    and if you have an ID field which is not duplicated (a unique field), you can use min() or max() to get one value for a duplicate set,

    "(select min(IDField),a,b,c from myProblemTable group by a,b,c)"
    LVL 15

    Accepted Solution

    select sum(inventory.qty), p.product
    from inventory
    inner join (select distinct product from problemtable) as p
    on inventory.product = p.product.

    Author Comment

    dbbishop and company.
    thank you all for your comments.  I'll try bishop's route and report back later tomorrow.
    LVL 31

    Expert Comment

    select sum(i.qty),p.product
    from inventory i,
    (select product, count(*)
     from problemtable
     group by product) p
    where i.product = p.product
    group by p.product;
    LVL 50

    Expert Comment

    you did not state which database system you are using ...
    the optimal syntax can vary between them...

    depending on what you actually want to achieve....

    either use a join...  to  a grouped or distinct subquery
    or use the exists condition in the where clause..

    select productid,sum(value)
    from producttable as  p
    where exists (select productid from problemtable as x
             on x.productid=p.productid)  
    group by productid

    Author Comment

    bishop's route worked.
    thanks a bunch!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This article describes some very basic things about SQL Server filegroups.
    Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
    Via a live example, show how to shrink a transaction log file down to a reasonable size.
    Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now