sql - JOIN Alternative to SELECT Subquery -


i trying convert select subquery join statement, works on netezza. working on oracle database following query worked fine, netezza not support subqueries in select statements. managed select subquery on statement, netezza doesn't support either.

my query trying find the daily revenue of individual product sales looking daily quantity sold , historical price date.

any suggestions on how decompose statement netezza accept? interested know if restructuring of query boost efficiency.

my original oracle sql query:

select     sales.date,     sales.product,     sales.quantity,     (         select price                 (             select product_id, price, max(eff_date) eff_date             hist_pricing                             product_id = sales.product ,                 sales.date > eff_date             group                 product_id, price         )     ) price,     (sales.quantity * price) revenue sales_records sales ; 

moved subquery join on statement:

select     sales.date,     sales.product,     sales.quantity,     h.price,     (sales.quantity * h.price) revenue sales_records sales left join hist_pricing h on     sales.product = h.product ,     sales.date =         (             select max(eff_date) most_recent             hist_pricing             sales.product = hist_pricing.product               , eff_date <= sales.date             group sales.product         ) 

for reference, here simplified example of table data looks like.

╔═════════════════════════════════════╗ ║           sales_records             ║ ╠═══════════╦═════════╦═══════════════╣ ║   date    ║ product ║ quantity_sold ║ ╠═══════════╬═════════╬═══════════════╣ ║ 1/1/2015  ║ shoes   ║           500 ║ ║ 2/5/2015  ║ shoes   ║          1200 ║ ║ 3/7/2015  ║ toys    ║           600 ║ ║ 3/9/2015  ║ shoes   ║           100 ║ ║ 5/10/2015 ║ hats    ║           400 ║ ╚═══════════╩═════════╩═══════════════╝ ╔══════════════════════════════╗ ║          hist_pricing        ║ ╠═══════════╦═════════╦════════╣ ║ eff_date  ║ product ║  price ║ ╠═══════════╬═════════╬════════╣ ║ 1/1/2015  ║ shoes   ║ $50    ║ ║ 1/1/2015  ║ toys    ║ $10    ║ ║ 1/1/2015  ║ hats    ║ $20    ║ ║ 2/15/2015 ║ shoes   ║ $45    ║ ║ 2/15/2015 ║ hats    ║ $15    ║ ║ 3/1/2015  ║ hats    ║ $20    ║ ║ 5/1/2015  ║ toys    ║ $15    ║ ║ 8/1/2015  ║ shoes   ║ $55    ║ ╚═══════════╩═════════╩════════╝ 

if can inline view in from clause... or, if have dba privileges create view, can this:

select     sales."date",     sales.product,     sales.quantity,     prices.price,     (sales.quantity * prices.price) revenue sales_records sales left join         (             select product_id, price, max(eff_date) eff_date             hist_pricing             group                 product_id, price         ) prices on prices.product_id = sales.product , prices.eff_date <= sales."date" ; 

otherwise, can this:

select     sales."date",     sales.product,     sales.quantity,     prices.price,     (sales.quantity * prices.price) revenue sales_records sales left join hist_pricing prices on prices.product_id = sales.product , prices.eff_date <= sales."date" not exists ( select 'later price product prior sales date'                      hist_pricing p2                     p2.product_id = prices.product_id                    ,    p2.eff_date <= sales."date"                    -- note: simple - assumes never have 2 prices same product on same date.                      -- if can happen, need adjust logic below include tie-breaker.                    ,    p2.eff_date > prices.eff_date ) ;                    

oracle has sorts of ways improve upon both of (e.g., max() keep). these 2 vanilla sql ways re-express original sql , rid of scalar subquery.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -