Hello.
I have a query which is pulling out several columns, using about 6 tables to produce an asset inventory list. (I'm using Oracle Financials tables actually).
I would like to join in a new column ("Purchase order number"
from a new table ("ASSET_INVOICES"
. I need to join using the "Asset ID" since this is the only link between the "ASSET_INVOICES" table and the others. The problem is that in the "ASSET_INVOICES" table there are usually 4 rows per asset and the "purchase order number" is not always there.
I am having difficulty joining in the table without multiplying the value of the assets by this number of rows.
What I am trying to achieve is to select the po_number once for the asset, if one exists, and just return a null/blank if it doesn't.
If I select the max(po_number) with a sub-select statement then it fails to select the rows which do not have a po_number at all in them. Am I joining incorrectly, and should I use some other outer join or something?
This is the bit in question:
I would be grateful if anyone can offer advice! [smiley]
I have a query which is pulling out several columns, using about 6 tables to produce an asset inventory list. (I'm using Oracle Financials tables actually).
I would like to join in a new column ("Purchase order number"
I am having difficulty joining in the table without multiplying the value of the assets by this number of rows.
What I am trying to achieve is to select the po_number once for the asset, if one exists, and just return a null/blank if it doesn't.
If I select the max(po_number) with a sub-select statement then it fails to select the rows which do not have a po_number at all in them. Am I joining incorrectly, and should I use some other outer join or something?
This is the bit in question:
Code:
ainv.po_number =
(SELECT max(ainv2.po_number)
FROM fa_asset_invoices ainv2
WHERE ainv2.asset_id = ad.asset_id)
I would be grateful if anyone can offer advice! [smiley]
Code:
SELECT
cc.segment2,
ad.asset_id,
ad.asset_number,
ad.asset_key_ccid,
ad.asset_type,
SUBSTR(ad.description, 1, 60) DESCRIP,
ad.manufacturer_name,
ad.serial_number,
ad.tag_number,
loc.segment1||'.'||
loc.segment2||'.'||
loc.segment3||'.'||
loc.segment4 LOCATION,
SUM(DECODE(dd.deprn_source_code, 'B', dd.addition_cost_to_clear, dd.cost)) COST,
(SUM(DECODE(dd.deprn_source_code, 'B', dd.addition_cost_to_clear, dd.cost)) - sum(dd.deprn_reserve)) NBV,
emp.full_name OWNER,
books.date_placed_in_service DATE_USE,
ainv.po_number po_number
FROM
fa_additions ad,
fa_locations loc,
gl_code_combinations cc,
fa_distribution_history dh,
fa_deprn_detail dd,
per_people_f emp,
fa_books books,
fa_asset_invoices ainv
WHERE
dh.asset_id = ad.asset_id
AND
dh.location_id = loc.location_id
AND
dh.code_combination_id = cc.code_combination_id
AND
dd.asset_id = dh.asset_id
AND
dh.assigned_to = emp.person_id(+)
AND
dd.asset_id = dh.asset_id
AND
dd.book_type_code = dh.book_type_code
AND
dd.distribution_id = dh.distribution_id
AND
books.asset_id = dh.asset_id
AND
ainv.po_number =
(SELECT min(ainv2.po_number)
FROM fa_asset_invoices ainv2
WHERE ainv2.asset_id = ad.asset_id)
AND
dd.period_counter =
(SELECT max (dd2.period_counter)
FROM fa_deprn_detail dd2
WHERE dd2.book_type_code = books.book_type_code and
dd2.asset_id = books.asset_id and
dd2.distribution_id = dd.distribution_id)
AND
cc.segment2 >= :P1 AND cc.segment2 <= :P2
GROUP BY
cc.segment2,
ad.asset_id,
ad.asset_number,
ad.asset_key_ccid,
ad.asset_type,
SUBSTR(ad.description, 1, 60),
ad.manufacturer_name,
ad.serial_number,
ad.tag_number,
loc.segment1||'.'||
loc.segment2||'.'||
loc.segment3||'.'||
loc.segment4,
emp.full_name,
books.date_placed_in_service,
ainv.po_number
ORDER BY 2