Solved

SQL query help

Posted on 2014-02-06
10
211 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
10 Comments
 
LVL 73

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
 
LVL 41

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 73

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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 31

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 73

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This article describes some very basic things about SQL Server filegroups.
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now