sql - Pass id to inner SELECT of nested query with ORDER BY and ROWNUM -
i facing similar problem, i had couple of months ago: need return single value subquery may or may not return list of values. thus, apply order by
first , select topmost entry where rownum=1
(yep, i'm on oracle).
the problem is, required id in inner query unknown due nesting.
last time, adviced use oracles analytic functions , apply min()
/max()
. time, however, i'm selecting currency symbol (varchar
) , cannot utilize these functions.
any appreciated. relevant part of query:
select myothercolumn, (select currencysymbol ( select distinct p2.id, p2.validfromdate, curr.currencysymbol currencysymbol my_order_tablepos op2 join my_order_table o2 on op2.fk_order=o2.id join my_price_table p2 on op2.fk_concretarticle=p2.fk_article join my_currency_table curr on p2.fk_currency=curr.id op2.fk_order=o.id , p2.id=( -- fails, since o.id unknown in inner query! -- determine current price article select max(p3.id) keep (dense_rank first order p3.mintoorder desc, p3.validfromdate desc) currentpriceid my_price_table p3 p3.fk_article=op2.fk_concretarticle , p3.validfromdate<=sysdate , p3.mintoorder<=op2.amount ) order p2.validfromdate desc ) rownum=1) currencysymbol ... o.id=786;
adding id inner query
select distinct p2.id, p2.validfromdate, curr.currencysymbol currencysymbol my_order_tablepos op2 join my_order_table o2 on op2.fk_order=o2.id join my_price_table p2 on op2.fk_concretarticle=p2.fk_article join my_currency_table curr on p2.fk_currency=curr.id op2.fk_order=786 , p2.id=( -- id set statically here -- determine current price article select max(p3.id) keep (dense_rank first order p3.mintoorder desc, p3.validfromdate desc) currentpriceid my_price_table p3 p3.fk_article=op2.fk_concretarticle , p3.validfromdate<=sysdate , p3.mintoorder<=op2.amount ) order p2.validfromdate desc;
gets me result:
id validfromdate currencysymbol 597 18.11.14 00:00:00,000000000 $ 556 10.11.14 10:54:05,973000000 € 552 10.11.14 10:44:25,719000000 € 258 15.10.14 10:12:01,670000000 €
my goal return currencysymbol
of topmost entry (here: $
).
so, question is: how can pass order id nested query or how can rewrite query such meets needs? know query overly complex, apologies that. :)
(select currencysymbol ( select op2.fk_order, curr.currencysymbol currencysymbol, row_number() over(partition op2.fk_order order p2.validfromdate desc) rw my_order_tablepos op2 join my_order_table o2 on op2.fk_order=o2.id join my_price_table p2 on op2.fk_concretarticle=p2.fk_article join my_currency_table curr on p2.fk_currency=curr.id p2.id=( -- determine current price article select max(p3.id) keep (dense_rank first order p3.mintoorder desc, p3.validfromdate desc) currentpriceid my_price_table p3 p3.fk_article=op2.fk_concretarticle , p3.validfromdate<=sysdate , p3.mintoorder<=op2.amount ) ) fk_order = o.id , rw=1) currencysymbol
Comments
Post a Comment