Solved

SQL 2005 Join problem

Posted on 2010-09-08
7
308 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

856 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