Solved

SQL 2005 Join problem

Posted on 2010-09-08
7
309 Views
Last Modified: 2012-05-10
I'm trying to use a query to provide a list of inventory items and their original sales date.

The first table SOP2 has all orders and the rows are by each item sold for the order.
The second table SOP1 is the order total with a single row per order.

The final table IV1 has most but not all item numbers that are in the SOP2 table

The problem is that when I run my query filtering out some items based on their category in IV1 and type from SOP1 I don't believe the results contain all the unique items that were sold are in the SOP2 table after the filter.

Below is my query

What should I be doing differently?



select min(sop1.docdate) as 'DocumentDate'
      ,max(sop2.itemnmbr) as 'ItemNumber'
      ,max(sop2.itemdesc) as 'ItemDescription'
from sop10200_sop30300_Union sop2
join sop10100_sop30200_Union sop1 on sop1.sopnumbe=sop2.sopnumbe
right join iv00101 iv1 on iv1.itemnmbr=sop2.itemnmbr
where
      sop1.soptype='2'
      and iv1.itmclscd<>'NON-IV'
group by sop2.itemnmbr
order by sop2.itemnmbr
0
Comment
Question by:jdr0606
[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
7 Comments
 
LVL 29

Expert Comment

by:QPR
ID: 33633507
you are aggregating items so would naturally be leaving some rows behind... unless I'm misreading this.
Can you post some sample data and some expected results?

0
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33633522
try and let me know result for


select min(sop1.docdate) as 'DocumentDate'
      ,max(sop2.itemnmbr) as 'ItemNumber'
      ,max(sop2.itemdesc) as 'ItemDescription'
from sop10200_sop30300_Union sop2
LEFT OUTER JOIN
sop10100_sop30200_Union sop1 on sop1.sopnumbe=sop2.sopnumbe
LEFT OUTER JOIN
iv00101 iv1 on iv1.itemnmbr=sop2.itemnmbr

where
      sop1.soptype='2'
      and iv1.itmclscd<>'NON-IV'
group by sop2.itemnmbr
order by sop2.itemnmbr

----------------------
Also for more clear understanding, what is requirement for iv1 table?
0
 

Author Comment

by:jdr0606
ID: 33633544
I'm running the query now, but for clarification the IV1 table is a partial item master and has the class code that we want to filter on so that the results don't include 'NON-IV' type items that were sold.

The SOP2 table has all sales items for the last 10 years however the IV1 table only has the last 5 years worth of items so there may be some items in the SOP2 table that don't have a corresponding item in the IV1 table.

Does that make sense?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33633559
Ok, So because your IV1 table has not all items, then why you were using right join? I think you should use left join.

Also you dont need to use Max for ItemNumber in select.

Other then that try to remove Group by and use Distinct after removing all max in select list, but after using Distinct in select list you need to think about order by.

Please let me know about query result.
0
 

Author Comment

by:jdr0606
ID: 33633560
Unfortunately the results were the same.
0
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33633566
Result may be same because possibly this time your IV1 table may have all records this time. isnt it?
0
 
LVL 1

Accepted Solution

by:
RobGoddard earned 500 total points
ID: 33635231
As QPR pointed out, aggregating the results will show only the max item number and the max description (and they most likely won't even relate to each other).  It's also very difficult to see what you need without some sample data.

Try the following though (QPR's answer).
select min(sop1.docdate) as 'DocumentDate'
      ,sop2.itemnmbr as 'ItemNumber'
      ,sop2.itemdesc as 'ItemDescription'
from sop10200_sop30300_Union sop2
join sop10100_sop30200_Union sop1 on sop1.sopnumbe=sop2.sopnumbe
right join iv00101 iv1 on iv1.itemnmbr=sop2.itemnmbr
where
      sop1.soptype='2'
      and iv1.itmclscd<>'NON-IV'
group by sop2.itemnmbr, sop2.itemdesc
order by sop2.itemnmbr

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

749 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