andremara
asked on
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.
sele sum(inventory.qty), problemtable.product
where inventory.product = problemtable.product.
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)"
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)"
"(select min(IDField),a,b,c from myProblemTable group by a,b,c)"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
dbbishop and company.
thank you all for your comments. I'll try bishop's route and report back later tomorrow.
Andre
thank you all for your comments. I'll try bishop's route and report back later tomorrow.
Andre
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;
from inventory i,
(select product, count(*)
from problemtable
group by product) p
where i.product = p.product
group by p.product;
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
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
ASKER
bishop's route worked.
thanks a bunch!
thanks a bunch!
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 =)