RE: accidentally creating duplicate data when querying 2 tables



As I've said in my other response you could only apply a parameter to a
column on the LEFT side of a LEFT OUTER JOIN, so while you'd be able to do so
in the case of a column in the 'primary' table you cannot do so in the case
of a column in the LineItems table. You can only do that with an INNER JOIN,
so if you want to apply parameters to both, and at the same time return rows
form the 'primary table where there are no matching LineItems rows then you'd
need to use a subquery, e.g.

SELECT CustomerID, OrderDate, NZ(ItemID,0) AS item_ID,
NZ(UnitPrice,0) AS Unit_Price, NZ(Quantity,0) As Quantity_Ordered
FROM Orders AS O1 LEFT JOIN LineItems AS LI1
ON O1.OrderID = LI1.OrderID
WHERE OrderDate >= [Enter date from:]
OR EXISTS
(SELECT *
FROM LineItems AS LI2
WHERE LI2.OrderID = O1.OrderID
AND Quantity > [Enter minimum quantity:]);

This would handle the rather unrealistic scenario where you want to return
all orders after a certain date, or orders of any date where the quantity of
any item ordered is greater than a specific quantity. Unrealistic as this
may be it does illustrate the principles involved.

Ken Sheridan
Stafford, England

"access hacker" wrote:

I simply have an inner join query and was thinking I needed to create a
different form of query. I have a primary table and a line item table that
is associated to it with the primary table's primary key. I am trying to
filter on a parameter value that will be in either the primary table or the
line item table. So I need it to say "show the record where "x" is found in
"COA" field or where "x" is found in Line Item Tb "LICOA" field. Yet, if "x"
is found two or more times in the Line item table, it tries to duplicate info
from the first table on the second row (record).

"akphidelt" wrote:

What kind of join do you have going on and how are the tables set up. If you
are getting the duplicate entries then you have a bad grouping set up or
something with how the query is set up. But typically you won't need a union
query to do most joins.

"access hacker" wrote:

I need to pull currency data from two tables yet when i create the query to
bring them together it creates a "cartesian product" (duplicates information
when there is one record in the parent table and 0 to many records in the
line item table). I read in a SQL for Dummies that you can create a "Union"
query and then do a "Coalesce" statement. Can this be done? Or is there
some way to get "Null" values for the records which don't have a record in
the other table?

.



Relevant Pages

  • Re: How to get a distinct count of result set of multople table joins?
    ... since the UNION syntax removes duplicate rows automatically. ...   "SORT " in your execution plan. ... The base query is an outer join. ...
    (comp.databases.oracle.misc)
  • Re: syntax for query to bring only one result per AccountNo
    ... My goal with the following query is to form a union of all fax numbers from ... different sources while avoiding any duplicate fax number listings. ... AccountNo DocID CompanyName PartNumber ...
    (microsoft.public.access.queries)
  • Re: Duplicate Amounts dont show up in Union Query
    ... I have 2 separate queries that show the correct data and number of entries. ... system so duplicate entries are possible. ... Anyone have any thoughts on this or can a Union Query just not handle exact ... Record the number of rows returned by the UNION ALL query. ...
    (microsoft.public.access.queries)
  • Re: How to get a distinct count of result set of multople table joins?
    ... each half of the UNION when the duplicates are removed. ... DBMS_XPLAN for the query. ... since the UNION syntax removes duplicate rows automatically. ...   "SORT " in your execution plan. ...
    (comp.databases.oracle.misc)
  • Re: duplicates query help & strategy for update queries with SetWarnings = False
    ... I'm calling a function that will set the occurence number. ... If there's a duplicate, ... I used the query wizard to create a "find duplicates" query, now I need to adjust the results of that query to add an occurence counter/number, and I'd be set. ... Once the user has handled all the problems, you enable the final command button at the bottom of the form, which executes an append query to add the data to the real table. ...
    (comp.databases.ms-access)

Quantcast