Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Joining three queries in SQL

Status
Not open for further replies.

MCubitt

Programmer
Mar 14, 2002
1,081
0
0
GB
I have 4 queries and need to amalgamate them to create one set of results.

The first:
Code:
select
 ipis.part_no,  ipis.contract, ipis.lot_batch_no, ipis.serial_no, ipis.qty_onhand 
from
 ifsapp.inventory_part_in_stock_tab ipis
where
 ipis.qty_onhand > 0

This is the primary query which should use the other two to obtain an additional column of cost.

I need to add
Code:
ipis.qty_onhand, cost

To get the cost I need to go to the transaction history table:
Code:
 select
 ith.order_no, ith.release_no, ith.sequence_no, ith.line_item_no
from
 ifsapp.inventory_transaction_hist_tab ith
where
 ith.transaction_code in ('NREC','ARRIVAL') and
 ith.order_type='PUR ORDER' and
 ith.part_no = ipis.part_no and
 ith.contract = ipis.contract and
 ith.lot_batch_no = ipis.lot_batch_no and
 ith.serial_no = ipis.serial_no

and this gives me the purchase order line, from where the cost is held:
Code:
select
 pol.buy_unit_price
from
 ifsapp.purchase_order_line_tab pol
where
 pol.order_no=ith.order_no and
 pol.line_no=ith.line_line_no

But if there is no transaction history record that matches the criteria OR no purchase order line, then it needs to use the standard cost:
Code:
select
 ip.standard_cost
from
 ifsapp.inventory_part ip
where
 ip.part_no = ipis.part_no and
 ip.contract = ipis.contract


I am really struggling - any offers please?





Applications Support
UK
 
I think I managed it after all!

Code:
select
 ipis.contract,  ipis.part_no, ipis.lot_batch_no, ipis.serial_no, ipis.qty_onhand, 
 nvl(
 (
 select
 (
 select
  pol.buy_unit_price
 from
  ifsapp.purchase_order_line_tab pol
 where
 rownum = 1 and
  pol.order_no=ith.order_no and
 pol.line_no=ith.line_item_no
 )
from
 ifsapp.inventory_transaction_hist_tab ith
where
 rownum='1' and 
 ith.order_type='PUR ORDER' and
 ith.part_no = ipis.part_no and
 ith.contract = ipis.contract and
 ith.lot_batch_no = ipis.lot_batch_no and
 ith.serial_no = ipis.serial_no
 )
,
 (
 select
  ipc.inventory_value
 from
  ifsapp.inventory_part_config_tab ipc
 where
  ipc.part_no = ipis.part_no and 
  ipc.contract=ipis.contract
 )
 )  Real_Cost
from
 ifsapp.inventory_part_in_stock_tab ipis
where
 ipis.qty_onhand > 0 
order by
 ipis.contract, ipis.part_no


Applications Support
UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top