Solved

SQL 2005 Join problem

Posted on 2010-09-08
7
300 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
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
Comment Utility
Unfortunately the results were the same.
0
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

762 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

11 Experts available now in Live!

Get 1:1 Help Now