Solved

SQL Help

Posted on 2006-11-14
4
2,598 Views
Last Modified: 2008-01-09
This is embarrasing but I'm having trouble with a SQL join I was hoping someone could assit with.

I'm writing a report (by item_id) that includes cost, po and invoice information. I'm fine with the item and cost data until I start joining to the po tables for the po data. Since I have to go to po_lines_all to complete the join from the item master to the po_header (for inventory_item_id), I'm now getting multiple records because of the individual po_lines. I don't see any way around this though. Can this not all be done through the same SQL statement?

Here is a small chunk of the code including the problematic join:

SELECT
msi.segment1                             item,
msi.organization_id                        org,
sum(qoh.transaction_quantity)                  qoh,            -- total quantity on hand
cic.item_cost                           item_cost,    -- frozen cost
max(poh.segment1)                        po_num        -- latest PO
FROM
mtl_system_items_b                         msi,
mtl_onhand_quantities                  qoh,
cst_item_costs                        cic,
po_headers_all                        poh,
po_lines_all                        pol
WHERE
msi.inventory_item_id          = qoh.inventory_item_id and
msi.inventory_item_id          = cic.inventory_item_id and
cic.cost_type_id                   = 1 and
cic.organization_id               = msi.organization_id and
pol.item_id                          = msi.inventory_item_id and
poh.po_header_id                = pol.po_header_id and
msi.organization_id              = 18 and
msi.inventory_item_id           = 2746
GROUP BY
msi.segment1,
msi.organization_id,
cic.item_cost
0
Comment
Question by:feign3
4 Comments
 
LVL 27

Expert Comment

by:sujith80
ID: 17938031
Does it help?

SELECT
msi.segment1                         item,
msi.organization_id                    org,
sum(qoh.transaction_quantity)               qoh,            -- total quantity on hand
cic.item_cost                       item_cost,    -- frozen cost
max(poh.segment1)                    po_num        -- latest PO
FROM
mtl_system_items_b                     msi,
mtl_onhand_quantities               qoh,
cst_item_costs                    cic,
po_headers_all                    poh,
(select distinct item_id, po_header_id  from po_lines_all  )                  pol
WHERE
msi.inventory_item_id          = qoh.inventory_item_id and
msi.inventory_item_id          = cic.inventory_item_id and
cic.cost_type_id                   = 1 and
cic.organization_id               = msi.organization_id and
pol.item_id                          = msi.inventory_item_id and
poh.po_header_id                = pol.po_header_id and
msi.organization_id              = 18 and
msi.inventory_item_id           = 2746
GROUP BY
msi.segment1,
msi.organization_id,
cic.item_cost
0
 
LVL 47

Expert Comment

by:schwertner
ID: 17938038
If I understand you correctly this join

poh.po_header_id                = pol.po_header_id

produces many lines.

You have to limit it somehow, e.g.

poh.po_header_id   IN (select  po_header_id from pol)
0
 
LVL 34

Accepted Solution

by:
Mark Geerlings earned 50 total points
ID: 17938221
Or, maybe you need a sub-query to get the max(po_header_id) from po_lines for that item, then remove the join to po_lines_all from the main query like this:

(Note that I also re-wrote the "where" clauses to put all of the known values on the right, and consistently use mtl_system_items as the "driving" table.)

SELECT
msi.segment1                         item,
msi.organization_id                    org,
sum(qoh.transaction_quantity)               qoh,            -- total quantity on hand
cic.item_cost                       item_cost,    -- frozen cost
max(poh.segment1)                    po_num        -- latest PO
FROM
mtl_system_items_b                     msi,
mtl_onhand_quantities               qoh,
cst_item_costs                    cic,
po_headers_all                    poh,
WHERE
qoh.inventory_item_id          = msi.inventory_item_id and
cic.inventory_item_id           =  msi.inventory_item_id and
cic.cost_type_id                   = 1 and
cic.organization_id               = msi.organization_id and
poh.po_header_id                = (select max pol.po_header_id from po_lines_all pol
                                              where pol.item_id = msi.inventory_item_id) and
msi.organization_id              = 18 and
msi.inventory_item_id           = 2746
GROUP BY
msi.segment1,
msi.organization_id,
cic.item_cost
0
 

Author Comment

by:feign3
ID: 17938458
Markgeer, yours was in the right ball park... It is the multiple po_line_id's that are producing the problem so I added:

pol.po_line_id = (select max(pol.po_line_id)
                         from po_lines_all pol
                         where pol.item_id = 2746)

This seems to have fixed things.

Thanks everyone!
0

Featured Post

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.

Join & Write a Comment

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

760 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

20 Experts available now in Live!

Get 1:1 Help Now