sql server - Optimize SQL Sub-Query for Max Value -


i have following query:

    select dlp.paramid paramid, dp.paramname paramname, dlp.locid locationid,    ml.locname locationname , di.entered_on dateentered,  dlp.freqdays frequency       data_locparams dlp      inner join data_input di on dlp.locid = di.locid     inner join data_parameters dp on dp.paramid = di.paramid     inner join map_locations ml on ml.locid =  dlp.locid     ( (dlp.freqdays not null)  , di.entered_on < (getutcdate() -  dlp.freqdays))     ,     di.entered_on = (select max(entered_on ) data_input locid = dlp.locid                      , paramid = dlp.paramid) 

i need assistance on how optimize query. bottleneck seems followng:

    di.entered_on = (select max(entered_on ) data_input locid = dlp.locid                      , paramid = dlp.paramid) 

note given enter_on, need max entered_on date based on locid , paramid.

i tried following did not intended result:

     select *      (         select dlp.paramid paramid, dp.paramname paramname, dlp.locid locationid,  ml.locname locationname, di.entered_on dateentered,  dlp.freqdays  frequency,             row_number() on (partition dlp.locid, dlp.paramid                        order di.entered_on desc)          rownum                     data_locparams dlp         inner join data_input di on dlp.locid = di.locid        inner join data_parameters dp on dp.paramid = di.paramid        inner join map_locations ml on ml.locid =  dlp.locid         dlp.freqdays not null               ) a.rownum = 1 , dateentered < (getutcdate() -   frequency) 

i think can replace window function on di table (done here subquery). note where clause not in subquery, because effects rows used max() (this problem in second query):

select dlp.paramid paramid, dp.paramname paramname, dlp.locid locationid,    ml.locname locationname , di.entered_on dateentered,  dlp.freqdays frequency       data_locparams dlp      inner join (select di.*,                        max(entered_on) on (partition locid, paramid) maxeo                 data_input di                ) di on dlp.locid = di.locid     inner join data_parameters dp on dp.paramid = di.paramid     inner join map_locations ml on ml.locid =  dlp.locid     (dlp.freqdays not null)  , di.entered_on = di.maxeo ,           di.entered_on < (getutcdate() -  dlp.freqdays) 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -