Search code examples
sqljoinviewinline

Inline Views with JOIN ON syntax possible?


I have two inline views that I can join via the classic join method. Results are as expected.

However, trying to adapt to JOIN ON syntax, I cannot see how. Restriction? WITH required? Or need to create two separate views and then apply - I presume will work. Cannot find anything in this regard.

The view is as follows, trying to get a match between BUY and SELL.

select BUY.*, SELL.* from (
select Z.*, 'LONG' from (
select X.commodity, X.market_place, X.max_qty, Y.maturity_dt, rank() over 
(partition by X.commodity, X.market_place order by y.maturity_dt ASC) as 
rank_val
  from
 (select commodity, market_place, max(qty) as max_qty
   from OPEN_POSITIONS
 where prod_type = 'future'
   and qty > 0
  group by commodity, market_place
 ) X,
  open_positions Y 
 where Y.qty = X.max_qty
   and Y.commodity = X.commodity
   and Y.prod_type = 'future'
   and Y.market_place = X.market_place ) Z
 where Z.rank_val = 1 ) BUY, 
  (
select Z.*, 'SHORT' from (
select X.commodity, X.market_place, X.min_qty, Y.maturity_dt, rank() over 
(partition by X.commodity, X.market_place order by y.maturity_dt ASC) as 
rank_val
  from
 (select commodity, market_place, min(qty) as min_qty
  from OPEN_POSITIONS
 where prod_type = 'future'
   and qty < 0
  group by commodity, market_place
 ) X,
  open_positions Y 
  where Y.qty = X.min_qty
   and Y.commodity = X.commodity
   and Y.prod_type = 'future'
   and Y.market_place = X.market_place ) Z
where Z.rank_val = 1) SELL
where BUY.commodity = SELL.commodity
  and BUY.market_place = SELL.market_place

Solution

  • Not possible, need to make a view first