• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

SQL 2005 Join problem

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
jdr0606
Asked:
jdr0606
1 Solution
 
QPRCommented:
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
 
Mohit VijayCommented:
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
 
jdr0606Author Commented:
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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
Mohit VijayCommented:
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
 
jdr0606Author Commented:
Unfortunately the results were the same.
0
 
Mohit VijayCommented:
Result may be same because possibly this time your IV1 table may have all records this time. isnt it?
0
 
RobGoddardCommented:
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

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now