Link to home
Start Free TrialLog in
Avatar of Sheldon Livingston
Sheldon LivingstonFlag for United States of America

asked on

SQL query help

The query below works perfect... except that it returns everything 6 times.  Each cost in TableA will be shown 6 times.  TableB is for Invoices and TableA contains the line items fo those invoices.  

Each invoice will have a unique Date and SEQ... each line item of that invoice will have the same Date and SEQ.

Any thoughts here?

SELECT
  TableA.Cost

FROM
  TableA
LEFT OUTER JOIN
  TableB
ON
  TableA.DATE=TableB.DATE AND TableA.SEQ=TableB.SEQ

WHERE
  TableA.JOB='ABC'
AND
  TableA.STATUS = 'Open'
Avatar of Sean Stuber
Sean Stuber

SELECT
  TableA.Cost
FROM
  TableA
WHERE
  TableA.JOB='ABC'
AND
  TableA.STATUS = 'Open';



I removed the LEFT JOIN because it doesn't contribute to the query except to create the multiples you're trying to avoid.


If B doesn't have a row, then, because it's an outer join, you'll still report the A row

If B does have a row, then, because of the join, for each row in B you'll get a copy of the A row.  

You could use DISTINCT or UNIQUE but that would simply hide the underlying problem of the join.  That is, you'd do all the work of the join, then throw away the results.  It's more efficient to simply NOT do the join at all.
So, which data you want to display, if the values are the same , you can use distinct claue

SELECT DISTINCT
  TableA.Cost

FROM
  TableA
LEFT OUTER JOIN
  TableB
ON
  TableA.DATE=TableB.DATE AND TableA.SEQ=TableB.SEQ

WHERE
  TableA.JOB='ABC'
AND
  TableA.STATUS = 'Open'
Avatar of Sheldon Livingston

ASKER

sdstuber:  Your query ignores the invoice data altogether.  I am building this query... we will later add a where clause to select invoices.

Aneesh:  Shouldn't need the distinct clause... something is wrong with the query to have it return everything 6 times.
If each invoice has 6 lines then it must appear 6 times on output in this query.

Let suppose each invoice has one or more lines. What should be the query result? To list each line or to calculate some sum from all invoice lines?

Another question: Do you even need info from invoice lines on output?

And one design question: Do you really have SEQ values unique for given date? Obvious scenario is to create some InvoiceID column which is unique in Invoice table and then it can be used for joins with invoice lines independently on date.

ne specific question to your query: If TableA represents invoice lines what means the COst column?  Is it the Cost specific for each invoice line? And if you sum Costs for invoice lines of one invoice do they represent the cost of this particular invoice?
sdstuber:  Your query ignores the invoice data altogether.  I am building this query... we will later add a where clause to select invoices.


I'm basing the modified query on the results you have described.  You had duplicated results because you only selected one column.  To fix that, remove the join.

If you want different results those would be based on new and different requirements.

For example,  include columns of B in your output and you'll see why the rows are duplicated.


SELECT tablea.cost, tableb.col1, tableb.col2
  FROM tablea LEFT OUTER JOIN tableb ON tablea.date = tableb.date AND tablea.seq = tableb.seq
 WHERE tablea.job = 'ABC' AND tablea.status = 'Open'
TableA contains:

$1 , 1/10/2014 , 2
$2 , 1/10/2014 , 2
$3 , 1/12/2014 , 4
$4 , 1/12/2014 , 4
$5 , 1/12/2014 , 4

TableB contains:

Inv1 , 1/10/2014 , 2
Inv2 , 1/10/2014 , 4

Does this make sense?
>>TableA contains:
$1 , 1/10/2014 , 2
$2 , 1/10/2014 , 2
$3 , 1/12/2014 , 4
$4 , 1/12/2014 , 4
$5 , 1/12/2014 , 4

TableB contains:
Inv1 , 1/10/2014 , 2
Inv2 , 1/10/2014 , 4<<
And what results do  you expect from your query?
1
2
3
4
5

Perhaps, at this point, not too useful, but each cost is itemized.
ASKER CERTIFIED SOLUTION
Avatar of Sean Stuber
Sean Stuber

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This lead me to figure out  the issue