Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Help

Posted on 2006-11-14
4
Medium Priority
?
2,658 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
[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
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 48

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 35

Accepted Solution

by:
Mark Geerlings earned 200 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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 information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

636 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