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
You can try something like below based on the database.
;With AllData AS
(
select commodity, market_place, qty
from OPEN_POSITIONS
where prod_type = 'future'
),
X AS
(
SELECT
commodity, market_place, max(qty) as max_qty
FROM AllData
WHERE qty > 0
group by commodity, market_place
),
Z AS
(
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 AllData Y INNER JOIN X ON Y.qty = X.max_qty AND Y.commodity = X.commodity AND Y.market_place = X.market_place
),
BUY AS
(
SELECT
commodity, market_place, max_qty, maturity_dt,rank_val
FROM Z
WHERE rank_val = 1
),
A AS
(
SELECT
commodity, market_place, max(qty) as max_qty
FROM AllData
WHERE qty < 0
group by commodity, market_place
),
B AS
(
SELECT
A.commodity, A.market_place, A.max_qty, Y.maturity_dt, rank() over
(partition by A.commodity, A.market_place order by y.maturity_dt ASC) as rank_val
FROM AllData Y INNER JOIN A ON Y.qty = A.max_qty AND Y.commodity = A.commodity AND Y.market_place = A.market_place
),
SELL AS
(
SELECT
commodity, market_place, max_qty, maturity_dt,rank_val
FROM B
WHERE rank_val = 1
)
SELECT
bu.*;sl.*
FROM BUY bu INNER JOIN SELL AS sl ON bu.commodity = sl.commodity
and bu.market_place = sl.market_place
不可能,需要先查看
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.