Solved

SQL query help

Posted on 2014-02-06
10
215 Views
Last Modified: 2014-02-24
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'
0
Comment
Question by:classnet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 39839998
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.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 39839999
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'
0
 

Author Comment

by:classnet
ID: 39840014
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.
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 42

Expert Comment

by:pcelba
ID: 39840017
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?
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39840022
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'
0
 

Author Comment

by:classnet
ID: 39840051
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?
0
 
LVL 32

Expert Comment

by:awking00
ID: 39840131
>>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?
0
 

Author Comment

by:classnet
ID: 39840141
1
2
3
4
5

Perhaps, at this point, not too useful, but each cost is itemized.
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 39840259
what are the join criteria?  Your sample data has 3 columns but your query references 4.  

Can you post sample data that corresponds to what you are doing?

Also note,  if your desired results do not include anything from tableb; then a join to tableb is the wrong solution.

Assuming you want at least something from B to be relevant in your results, please expand your test case to illustrate.
0
 

Author Closing Comment

by:classnet
ID: 39882600
This lead me to figure out  the issue
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

733 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