Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL query help

Posted on 2014-02-06
10
Medium Priority
?
223 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 43

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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

636 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