Question

EDITED: I fixed the mis-matched query and data

I'm using Visual Basic to query a SQL Server database to try to make a program to generate a report for a client. They already have report generating software, but it doesn't do what they want. I'm trying to join data from 3 different tables in one query, and I've come up with the following (I've removed the irrelevant fields from the query):

SELECT lineitem.lineitemid,
       lineitem.cost,
       lineitem.sale,
       partitem.partitemid,
       partitem.lineitemid,
       partitem.quantity,
       partitem.partid,
       part.partid,
       part.cost,
       part.description,
       inventorytransaction.partid,
       inventorytransaction.cost,
       inventorytransaction.orderdate
FROM   sm.lineitem  
       RIGHT OUTER JOIN sm.partitem 
         ON partitem.lineitemid = lineitem.lineitemid 
       RIGHT OUTER JOIN sm.part
         ON partitem.partid = part.partid
      RIGHT OUTER JOIN sm.inventorytransaction
         ON part.partid = inventorytransaction.partid
WHERE  lineitem.lineitemid > 62421 AND lineitem.lineitemid < 62442

The problem is that it returns 100+ values for the same id. Here's a sample from the actual query:

62422   2.23    4.99    31964   1   2   OIL FILTER  2   12/10/2010 19:00
62422   2.23    4.99    31964   1   2   OIL FILTER  2   10/20/2010 19:00
62422   2.23    4.99    31964   1   2   OIL FILTER  2   9/1/2010 19:00
62422   2.23    4.99    31964   1   2   OIL FILTER  2   8/26/2010 19:00
62422   2.23    4.99    31964   1   2   OIL FILTER  2   8/13/2010 19:00
62422   2.23    4.99    31964   1   2   OIL FILTER  2   7/2/2010 19:00
62422   2.23    4.99    31964   1   2   OIL FILTER  2   6/4/2010 19:00
62422   2.23    4.99    31964   1   2   OIL FILTER  2   5/20/2010 19:00
62422   2.23    4.99    31964   1   2   OIL FILTER  2   4/29/2010 19:00
62422   2.23    4.99    31964   1   2   OIL FILTER  2   3/19/2010 19:00
62422   2.23    4.99    31964   1   2   OIL FILTER  2   2/25/2010 19:00
62422   2.23    4.99    31964   1   2   OIL FILTER  2   1/29/2010 19:00
62422   2.23    4.99    31964   1   2   OIL FILTER  2   12/31/2009 19:00

If I run SELECT * from lineitem it returns unique values:

362440  25.44   -318715 -318715
362441  0   -318716 -318716
362442  15.96   -318717 -318717
362443  0   -318718 -318718
362444  13.5    -318719 -318719
362445  1.65    -318720 -318720
362446  0   -318721 -318721
362447  0   -318722 -318722
362448  0   -318723 -318723
362449  0   -318724 -318724

What's wrong? What other information do you need to answer the question?

I've tried several variations of JOIN to no avail. UNION won't work because of conflicting data types and I need info that is unique to specific tables.

Side note: I didn't design the database, If I had, I wouldn't need to query 3 different tables this way to do what they want.

Was it helpful?

Solution

Thanks for the help guys. I talked to a co-worker and we figured it out. It's a case of having a 1-to-n relationship as a number of you suggested. I guess I didn't understand the database. It turns out that each LineItemId is related to a transaction that may have multiple Parts associated with it. The way I ended up narrowing it down is by date. The final query is the same as the one in the question but with WHERE inventorytransaction.orderdate > 'some-date' AND < 'some-other-date' tacked on to the end. I also dropped the RIGHT OUTER JOIN in favor of a normal JOIN. Now I have to wrap all this in a usable interface and ship it off to the customer :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top