• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1921
  • Last Modified:

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

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.
0
andremara
Asked:
andremara
1 Solution
 
MrRobotCommented:
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 =)
0
 
MrRobotCommented:
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)"
0
 
MrRobotCommented:
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)"
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
dbbishopCommented:
select sum(inventory.qty), p.product
from inventory
inner join (select distinct product from problemtable) as p
on inventory.product = p.product.
0
 
andremaraAuthor Commented:
dbbishop and company.
thank you all for your comments.  I'll try bishop's route and report back later tomorrow.
Andre
0
 
awking00Commented:
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;
0
 
LowfatspreadCommented:
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
0
 
andremaraAuthor Commented:
bishop's route worked.
thanks a bunch!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now