[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

SQL 2005 Join problem

Posted on 2010-09-08
7
Medium Priority
?
314 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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 2000 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

656 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