[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

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'
0
classnet
Asked:
classnet
1 Solution
 
sdstuberCommented:
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
 
Aneesh RetnakaranDatabase AdministratorCommented:
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
 
classnetAuthor Commented:
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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
pcelbaCommented:
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
 
sdstuberCommented:
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
 
classnetAuthor Commented:
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
 
awking00Commented:
>>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
 
classnetAuthor Commented:
1
2
3
4
5

Perhaps, at this point, not too useful, but each cost is itemized.
0
 
sdstuberCommented:
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
 
classnetAuthor Commented:
This lead me to figure out  the issue
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now