{"id":220455,"date":"2014-04-23T00:24:03","date_gmt":"2014-04-22T20:24:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=220455"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=220455","title":{"rendered":"<span class=\"post_title\">\u00abDense_rank()\u00bb vs \u00abMax()\u00bb \u0438\u043b\u0438 \u0440\u0430\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u044b\u043c \u043a\u043e\u043d\u0446\u043e\u043c<\/span>"},"content":{"rendered":"<div class=\"content html_format\"> \t\t\t\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435, \u043a\u043e\u043b\u043b\u0435\u0433\u0438.<br \/>  \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e \u0441\u0432\u043e\u0438\u0445 \u0438\u0437\u044b\u0441\u043a\u0430\u043d\u0438\u044f\u0445 \u0432 \u0432\u043e\u043f\u0440\u043e\u0441\u0435: \u00ab\u0410 \u0447\u0442\u043e \u0436\u0435 \u043b\u0443\u0447\u0448\u0435: dense_rank() \u0438\u043b\u0438 max()\u00bb \u0438, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u043f\u043e\u0447\u0435\u043c\u0443 \u044d\u0442\u0438 \u0438\u0437\u044b\u0441\u043a\u0430\u043d\u0438\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u0438\u0441\u044c \u0441 \u043d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u044b\u043c, \u043f\u043e \u043a\u0440\u0430\u0439\u043d\u0435\u0439 \u043c\u0435\u0440\u0435 \u0434\u043b\u044f \u043c\u0435\u043d\u044f, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h5>\u041f\u0440\u0435\u0434\u044b\u0441\u0442\u043e\u0440\u0438\u044f:<\/h5>\n<p> \u0422\u0430\u043a \u0441\u043b\u043e\u0436\u0438\u043b\u0438\u0441\u044c \u0437\u0432\u0435\u0437\u0434\u044b, \u0447\u0442\u043e \u043c\u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u0438\u0441\u043a\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443. \u041f\u0435\u0440\u0435\u0434 \u043a\u0430\u0436\u0434\u044b\u043c \u0441\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u044f \u0438\u0437\u0443\u0447\u0430\u044e \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u044e, \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u0435\u043d\u044f \u043f\u0440\u0438\u0433\u043b\u0430\u0441\u0438\u043b\u0438, \u0434\u0430\u0431\u044b \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u0447\u0435\u043c \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u044f, \u0447\u0435\u043c\u0443 \u044f \u043d\u0430\u0443\u0447\u0443\u0441\u044c \u0435\u0441\u043b\u0438 \u043c\u043d\u0435 \u0441\u0434\u0435\u043b\u0430\u044e\u0442 \u043e\u0444\u0444\u0435\u0440 \u0438 \u0442.\u0434. \u0418 \u0432\u043e\u0442, \u0432 \u043e\u0434\u0438\u043d \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442, \u043c\u043d\u0435 \u043f\u0440\u0438\u0448\u043b\u043e \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0435\u043d\u0438\u0435 \u043d\u0430 \u0438\u043d\u0442\u0435\u0440\u0432\u044c\u044e, \u043d\u0430 \u043f\u043e\u0437\u0438\u0446\u0438\u044e PL\/SQL \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430, \u043e\u0442 \u043e\u0434\u043d\u043e\u0439 \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u043e\u0439 \u043a\u043e\u043c\u043f\u0430\u043d\u0438\u0438. \u041f\u0440\u043e\u0447\u0438\u0442\u0430\u0432 \u043e \u043d\u0435\u0439, \u043c\u043d\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c, \u0447\u0442\u043e \u044f \u0432\u043b\u044e\u0431\u043b\u0435\u043d \u0438 \u0445\u043e\u0447\u0443 \u0442\u0430\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c. \u041a\u043e\u0433\u0434\u0430 \u0436\u0435 \u044f \u043f\u0440\u0438\u0448\u0435\u043b \u043d\u0430 \u0441\u0430\u043c\u043e \u0438\u043d\u0442\u0435\u0440\u0432\u044c\u044e \u0438 \u0432 \u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442, \u043a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u043a \u0438\u043d\u0442\u0435\u0440\u0432\u044c\u044e \u0443\u0436\u0435 \u0433\u043e\u0442\u043e\u0432\u043e, \u0430 \u043e\u043d\u043e \u0435\u0449\u0435 \u043d\u0435 \u043d\u0430\u0447\u0430\u043b\u043e\u0441\u044c \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043b\u044e\u0434\u0438 \u0437\u043d\u0430\u043a\u043e\u043c\u044f\u0442\u0441\u044f, hr \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442 \u043a\u043e\u0444\u0435 \u0438 \u0442.\u0434., \u044f \u0443\u0436\u0435 \u043f\u043e\u043d\u044f\u043b, \u0447\u0442\u043e \u0445\u043e\u0447\u0443, \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0447\u0443, \u0442\u0443\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c. <br \/>  \u0417\u0430 \u0432\u0441\u0435 \u0438\u043d\u0442\u0435\u0440\u0432\u044c\u044e \u0441 \u0442\u0438\u043c\u043b\u0438\u0434\u043e\u043c, \u043c\u043d\u0435 \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u043b\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441, \u0432 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u043c \u0436\u0435 \u0432\u0441\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u043b\u043e \u0432 \u0440\u0435\u0436\u0438\u043c\u0435: \u0432\u043e\u043f\u0440\u043e\u0441-\u043e\u0442\u0432\u0435\u0442.<br \/>  \u0417\u0430\u0434\u0430\u0447\u043a\u0430 \u0437\u0432\u0443\u0447\u0430\u043b\u0430 \u0442\u0430\u043a:<br \/>  \u00ab\u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u0432 \u043d\u0435\u0439 4 \u0441\u0442\u043e\u043b\u0431\u0446\u0430: id \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, id \u043a\u043b\u0438\u0435\u043d\u0442\u0430, \u0434\u0430\u0442\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u0441\u0443\u043c\u043c\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438. \u041d\u0443\u0436\u043d\u043e \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0443\u043c\u043c\u043e\u0439 \u0437\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u043f\u0435\u0440\u0438\u043e\u0434\u00bb.<br \/>  \u0418 \u044f, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u043d\u0430\u0447\u0430\u043b \u043b\u0438\u0445\u043e\u0440\u0430\u0434\u043e\u0447\u043d\u043e \u0441\u043e\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c, \u043a\u0430\u043a \u0436\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u043e \u043a\u0440\u0430\u0441\u0438\u0432\u043e \u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e. \u0410 \u043a\u0440\u043e\u043c\u0435 \u043e\u0440\u0430\u043a\u043b\u0430 \u044f \u0435\u0449\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u043b \u0441 \u0442\u0435\u0440\u0430\u0434\u0430\u0442\u043e\u0439 \u0438 \u0432 \u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u043c\u043e\u0439 \u043c\u043e\u0437\u0433 \u0432\u044b\u0434\u0430\u043b \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">\/*id \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 - oper_id, id \u043a\u043b\u0438\u0435\u043d\u0442\u0430 - client_id, \u0434\u0430\u0442\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 - input_date, \u0441\u0443\u043c\u043c\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 - amount*\/ select t.* \t, max(t.amount) over (partition by t.client_id) as m_a from some_table t qualify oper_id = max(t.oper_id) over (partition by t.client_id)  where m_a = amount      <\/code><\/pre>\n<\/div>\n<\/div>\n<p>\u0418 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441 \u0442\u0435\u0440\u0430\u0434\u0430\u0442\u043e\u0439 \u0442\u0430\u043a\u043e\u0435 \u0431\u044b \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u043e, \u043d\u043e \u043d\u0435 \u0441 \u043e\u0440\u0430\u043a\u043b\u043e\u043c, \u0443\u0432\u044b. \u0418 \u0447\u0435\u0442\u043a\u043e \u043f\u043e\u043c\u043d\u044f, \u0447\u0442\u043e \u00abqualify\u00bb \u0432 \u043e\u0440\u0430\u043a\u043b\u0435 \u043d\u0435\u0442, \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u043d\u0430 \u0431\u0443\u043c\u0430\u0436\u043a\u0435 \u0447\u0442\u043e \u0442\u043e \u043d\u0430 \u043f\u043e\u0434\u043e\u0431\u0438\u0438:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">select t.* from some_table t where amount = max(t.oper_id) over (partition by t.client_id)  <\/code><\/pre>\n<\/div>\n<\/div>\n<p>\u041d\u0430 \u0447\u0442\u043e \u043c\u043d\u0435 \u0431\u044b\u043b \u0437\u0430\u0434\u0430\u043d \u0432\u043e\u043f\u0440\u043e\u0441: \u00ab\u0410 \u043f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b\u043b\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0430 max(), \u0430 \u043d\u0435 dense_rank()?\u00bb, \u043d\u0435 \u043f\u043e\u043c\u043d\u044e \u0447\u0442\u043e \u044f \u0442\u043e\u0447\u043d\u043e \u043e\u0442\u0432\u0435\u0442\u0438\u043b, \u043d\u043e \u0437\u0432\u0443\u0447\u0430\u043b\u043e \u044d\u0442\u043e \u043f\u0440\u0438\u0431\u043b\u0438\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0442\u0430\u043a: \u00abmax() \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u0447\u0430\u0449\u0435 \u0438 \u043c\u043e\u0433\u0443, \u0431\u043e\u043b\u0435\u0435-\u043c\u0435\u043d\u0435\u0435 \u0442\u043e\u0447\u043d\u043e, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c, \u0447\u0442\u043e \u043e\u043d \u043c\u043d\u0435 \u0432\u0435\u0440\u043d\u0435\u0442, \u0432 \u043e\u0442\u043b\u0438\u0447\u0438\u0438 \u043e\u0442 dense_rank()\u00bb. \u0414\u0430\u043b\u044c\u0448\u0435 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0432\u044c\u044e \u043d\u0435 \u0431\u0443\u0434\u0443, \u0441\u043a\u0430\u0436\u0443 \u0442\u043e\u043b\u044c\u043a\u043e, \u0447\u0442\u043e \u043c\u043d\u0435, \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435, \u043e\u0442\u043a\u0430\u0437\u0430\u043b\u0438. \u041f\u043e\u0437\u0436\u0435, \u0443\u0436\u0435 \u0434\u043e\u043c\u0430, \u0432 \u043f\u043e\u043f\u044b\u0442\u043a\u0430\u0445 \u043f\u0440\u043e\u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0438 \u043f\u043e\u043d\u044f\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438, \u044f \u043f\u0440\u0438\u0448\u0435\u043b \u043a \u0432\u044b\u0432\u043e\u0434\u0443, \u0447\u0442\u043e \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0441\u0438\u043b\u044c\u043d\u043e \u0445\u043e\u0442\u0435\u043b \u0442\u0430\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0438 \u043f\u0435\u0440\u0435\u0432\u043e\u043b\u043d\u043e\u0432\u0430\u043b\u0441\u044f, \u0438\u043d\u0430\u0447\u0435 \u0442\u0443 \u043a\u0430\u0448\u0443, \u0442\u0432\u043e\u0440\u0438\u0432\u0448\u0443\u044e\u0441\u044f \u0443 \u043c\u0435\u043d\u044f \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0438\u043d\u0442\u0435\u0440\u0432\u044c\u044e, \u044f \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u0442\u044c \u043d\u0435 \u043c\u043e\u0433\u0443. \u042d\u0442\u043e \u0431\u044b\u043b\u043e \u043d\u0435\u0447\u0442\u043e \u0441\u0440\u043e\u0434\u043d\u043e\u0435 \u0441 \u0442\u0435\u043c \u043e\u0449\u0443\u0449\u0435\u043d\u0438\u0435\u043c, \u043a\u043e\u0433\u0434\u0430 \u0448\u043a\u043e\u043b\u044c\u043d\u0438\u043a \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u0441 \u0434\u0435\u0432\u043e\u0447\u043a\u043e\u0439, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043e\u043d \u0442\u0430\u0439\u043d\u043e \u043b\u044e\u0431\u0438\u0442 \u0435\u0449\u0435 \u0441 \u0441\u0430\u0434\u0438\u043a\u0430, \u043d\u043e \u044d\u0442\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0438 \u0432\u0441\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u0441\u0442\u0430\u0432\u044f\u0442 \u0435\u0433\u043e \u0432 \u043d\u0435\u043b\u043e\u0432\u043a\u043e\u0435 \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435. \u0422\u0430\u043a \u0436\u0435 \u0438 \u044f, \u0441\u0442\u0430\u0440\u0430\u044f\u0441\u044c \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0441\u043f\u043e\u043a\u043e\u0439\u043d\u044b\u043c \u0438 \u0430\u0434\u0435\u043a\u0432\u0430\u0442\u043d\u044b\u043c, \u043f\u043e\u043a\u0430\u0437\u0430\u043b \u0441\u0435\u0431\u044f \u043a\u0430\u043a \u043d\u0438\u043a\u0443\u0434\u044b\u0448\u043d\u044b\u0439 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0441\u0442. \u0412 \u043e\u0431\u0449\u0435\u043c, \u044f \u0440\u0435\u0448\u0438\u043b \u0434\u043b\u044f \u0441\u0435\u0431\u044f \u0432\u044b\u044f\u0441\u043d\u0438\u0442\u044c, \u0447\u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c dense_rank() \u0438\u043b\u0438 max() \u043f\u0440\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u0442\u0430\u043a\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<h5>\u0418\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435<\/h5>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c\u0438 \u0433\u043b\u0430\u0437\u0430\u043c\u0438 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0432\u0441\u0435, \u0447\u0442\u043e \u044f \u043d\u0430\u043f\u0438\u0448\u0443 \u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c\u0438 \u0440\u0443\u043a\u0430\u043c\u0438 \u044d\u0442\u043e \u0432\u0441\u0435 \u043f\u043e\u0442\u0440\u043e\u0433\u0430\u0442\u044c \u2014 \u044f \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u043b \u043d\u0430\u0431\u043e\u0440 \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0430:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">\/*\u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0447\u043a\u0443*\/ create table habr_test_table_220414 (   oper_id    number,   client_id  number,   input_date date,   amount     number   constraint habr_test_table_220414_pk primary key (oper_id) ); grant all on habr_test_table_220414 to public;  \/*\u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0435 oper_id \u0431\u044b\u043b\u043e \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u043c - \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c*\/ create sequence habr_test_sequence_220414 increment by 1; grant all on habr_test_sequence_220414 to public; \/*\u0438 \u0442\u0440\u0438\u0433\u0433\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0435 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0434\u043c\u0435\u043d\u044f\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 oper_id \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438*\/ create trigger habr_test_trigger_220414 before insert    on habr_test_table_220414    for each row begin    :new.oper_id := habr_test_sequence_220414.nextval; end;  \/*\u041d\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0447\u043a\u0443 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0438 \u0434\u0430 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043c \u0440\u0430\u043d\u0434\u043e\u043c \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0435\u0435 \u0440\u0430\u0437\u043d\u043e\u043e\u0431\u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438*\/ \/*\u0414\u043b\u044f \u0442\u0435\u0441\u0442\u0430 \u043f\u0440\u0438\u043c\u0435\u043c \u043f\u0435\u0440\u0438\u043e\u0434 \u0432 \u0433\u043e\u0434, \u0442\u0430\u043a\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u0441 10-\u0442\u044c\u044e \u043a\u043b\u0438\u0435\u043d\u0442\u0430\u043c\u0438*\/ \/*\u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u043c \u043d\u0435 \u0432\u044b\u0434\u0430\u0432\u0430\u043b\u0441\u044f cost = 3, \u043d\u0430\u043f\u043e\u043b\u043d\u0438\u043c \u0442\u0430\u0431\u043b\u0438\u0447\u043a\u0443 20000 \u0441\u0442\u0440\u043e\u043a, \u0435\u0441\u043b\u0438 \u043a\u043e\u043c\u0443 \u0442\u043e \u0445\u043e\u0447\u0435\u0442\u0441\u044f*\/ \/*\u0431\u043e\u043b\u044c\u0448\u0435 \u0438\u043b\u0438 \u043c\u0435\u043d\u044c\u0448\u0435 - \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435 \u0436\u0435\u043b\u0430\u0435\u043c\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u043f\u043e\u043b\u0430\u043c \u0438 \u043f\u0440\u0438\u0441\u0432\u043e\u0439\u0442\u0435 \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 counter *\/ declare   counter number := 10000;   i       number := 0; begin    loop    insert into habr_test_table_220414        (          client_id        , input_date        , amount       )     values       (          trunc (dbms_random.value (1, 11))        , to_date(trunc(dbms_random.value(to_char(date '2013-01-01','j'),to_char(date '2013-12-31','j'))),'j')        , trunc (dbms_random.value (1, 100000))       );    exit when (i = counter);     i := i + 1;    end loop;    commit; \/*\u0414\u0430\u0431\u044b \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u0435 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0438\u043c\u0435\u0435\u043c \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0435 \u0441\u0443\u043c\u043c\u044b \u043f\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c \u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0430*\/ \/*\u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0442\u0430\u043a\u0443\u044e \u043a\u043e\u043c\u0430\u043d\u0434\u0443:*\/   insert into habr_test_table_220414 select * from habr_test_table_220414;   commit; \/*\u0430 \u043e \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 id \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0437\u0430\u0431\u043e\u0442\u0438\u0442\u0441\u044f \u0442\u0440\u0438\u0433\u0433\u0435\u0440*\/ end;<\/code><\/pre>\n<\/div>\n<\/div>\n<p>\u0418\u0442\u0430\u043a, \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u044b, \u043f\u043e\u0440\u0430 \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0430\u0442\u044c \u043a, \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0441\u0430\u043c\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c. \u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043e\u0431\u0440\u0435\u0437\u0430\u0442\u044c \u043d\u0430\u0448\u0438 20000 \u0441\u0442\u0440\u043e\u043a, \u043c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0442\u044c \u043d\u0430\u0448\u0443 \u0432\u044b\u0431\u043e\u0440\u043a\u0443 \u043a\u0430\u043a\u0438\u043c-\u0442\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c \u043f\u0435\u0440\u0438\u043e\u0434\u043e\u043c, \u043d\u0430\u043c \u0432\u0435\u0434\u044c \u0432\u0430\u0436\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u044c \u043a\u0430\u043a\u043e\u0439 \u043c\u0435\u0442\u043e\u0434 \u043b\u0443\u0447\u0448\u0435 \u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0435\u0435, \u0430 <\/p>\n<pre><code class=\"sql\">where input_date between to_date('01.01.2013','dd.mm.yyyy') and to_date('01.05.2013','dd.mm.yyyy')<\/code><\/pre>\n<p> \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0438 \u043f\u043e\u0442\u043e\u043c.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0417\u0430\u043f\u0440\u043e\u0441 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c max()<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">select * from (     select c.*         , max(c.oper_id) over (partition by c.client_id) as m_o\/*max_operation*\/     from         (         select t.*         , max(t.amount) over (partition by t.client_id) as m_a\/*max_amount*\/         from habr_test_table_220414 t         ) c      where c.m_a = c.amount ) where m_o = oper_id; <\/code><\/pre>\n<\/div>\n<\/div>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0417\u0430\u043f\u0440\u043e\u0441 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c dense_rank()<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">select * from (     select c.*         , dense_rank() over (partition by c.client_id order by c.oper_id desc) as m_o\/*max_operation*\/     from         (         select t.*         , dense_rank() over (partition by t.client_id order by t.amount desc) as m_a\/*max_amount*\/         from habr_test_table_220414 t         ) c      where c.m_a = 1 ) where m_o = 1;<\/code><\/pre>\n<\/div>\n<\/div>\n<p>\u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u043b\u0430\u043d\u044b \u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 (\u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043e \u0432 pl\/sql developer):<br \/>  Max:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442<\/b><\/p>\n<div class=\"spoiler_text\"><img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/687\/044\/819\/6870448191c492151933e633f7597f4a.png\" alt=\"image\"\/><\/div>\n<\/div>\n<p>Dense_rank:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442<\/b><\/p>\n<div class=\"spoiler_text\"><img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/414\/2ff\/f82\/4142fff8297a649af85eab30e6d83364.png\" alt=\"image\"\/><\/div>\n<\/div>\n<p>\u041d\u043e \u044d\u0442\u043e \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u043b\u0430\u043d\u044b, \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0443\u0442\u0438\u043b\u0438\u0442\u044b SQLTUNE:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430:<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">\/*\u0414\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441 max()*\/ DECLARE my_task_name varchar2(30);my_sqltext clob;rep_tuning   clob; BEGIN     Begin          DBMS_SQLTUNE.DROP_TUNING_TASK('my_sql_tuning_task_max');         exception when others then NULL;      end;     MY_SQLTEXT:=          'select * from         (             select c.*                 , max(c.oper_id) over (partition by c.client_id) as m_o\/*max_operation*\/             from                 (                 select t.*                 , max(t.amount) over (partition by t.client_id) as m_a\/*max_amount*\/                 from habr_test_table_220414 t                 ) c              where c.m_a = c.amount         ) where m_o = oper_id';     MY_TASK_NAME:=DBMS_SQLTUNE.CREATE_TUNING_TASK(SQL_TEXT =&gt; my_sqltext,     TIME_LIMIT =&gt; 60,               --\u0437\u0430\u0434\u0430\u0435\u0442\u0441\u044f \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445     TASK_NAME =&gt;'my_sql_tuning_task_max', DESCRIPTION=&gt; my_task_name ,     SCOPE      =&gt; DBMS_SQLTUNE.scope_comprehensive);     begin         DBMS_SQLTUNE.EXECUTE_TUNING_TASK('my_sql_tuning_task_max');         exception when others then null;     end; END;      \/*\u0414\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441 dense_rank()*\/ DECLARE     my_task_name varchar2(30);my_sqltext clob;rep_tuning   clob;     BEGIN     Begin          DBMS_SQLTUNE.DROP_TUNING_TASK('my_sql_tuning_task_dense');         exception when others then NULL;      end;     MY_SQLTEXT:=          'select * from         (             select c.*                , dense_rank() over (partition by c.client_id order by c.oper_id desc) as m_o\/*max_operation*\/             from                 (                 select t.*                 , dense_rank() over (partition by t.client_id order by t.amount desc) as m_a\/*max_amount*\/                 from habr_test_table_220414 t                 ) c              where c.m_a = 1         ) where m_o = 1';     MY_TASK_NAME:=DBMS_SQLTUNE.CREATE_TUNING_TASK(SQL_TEXT =&gt; my_sqltext,     TIME_LIMIT =&gt; 60,               --\u0437\u0430\u0434\u0430\u0435\u0442\u0441\u044f \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445     TASK_NAME =&gt;'my_sql_tuning_task_dense', DESCRIPTION=&gt; my_task_name ,     SCOPE      =&gt; DBMS_SQLTUNE.scope_comprehensive);     begin         DBMS_SQLTUNE.EXECUTE_TUNING_TASK('my_sql_tuning_task_dense');         exception when others then null;     end; END; \/*\u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u0447\u0442\u043e \u0442\u043e \u0441\u043b\u043e\u043c\u0430\u043b\u043e\u0441\u044c \u0438 \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 \u044d\u0442\u043e\u043c \u043a\u0443\u0441\u043a\u0435 \u043a\u043e\u0434\u0430 \u0440\u0430\u0437\u0443\u043a\u0440\u0430\u0448\u0435\u043d\u0430 \u0446\u0432\u0435\u0442\u043e\u043c \u043a\u0430\u043a \u0441\u0442\u0440\u043e\u043a\u0430*\/ \/*\u043f\u0440\u043e\u0448\u0443 \u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f, \u043d\u0435 \u043e\u0431\u0440\u0430\u0449\u0430\u0439\u0442\u0435 \u043d\u0430 \u044d\u0442\u043e \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435*\/<\/code><\/pre>\n<\/div>\n<\/div>\n<p> \u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u044d\u0442\u0438 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u043b\u0430\u043d\u044b \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">SELECT DBMS_SQLTUNE.REPORT_TUNING_TASK('my_sql_tuning_task_max') FROM DUAL;<\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/56b\/6e8\/13a\/56b6e813aaf84feb6a39d0bd8e2d3669.png\" alt=\"image\"\/>  <\/p>\n<pre><code class=\"sql\">SELECT DBMS_SQLTUNE.REPORT_TUNING_TASK('my_sql_tuning_task_dense') FROM DUAL;<\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/beb\/32c\/1e0\/beb32c1e0669c362bbcaaf701198192c.png\" alt=\"image\"\/><\/div>\n<\/div>\n<p>\u041f\u043e\u043c\u0438\u043c\u043e \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u043b\u0430\u043d\u0430 SQLTUNE \u0442\u0430\u043a\u0436\u0435 \u0432\u044b\u0434\u0430\u0435\u0442 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0438 \u043f\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u043a\u0440\u0438\u043f\u0442\u0430, \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u043d \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442 \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0443, \u043d\u043e \u0442\u0430\u043a \u043a\u0430\u043a \u0442\u0430\u0431\u043b\u0438\u0447\u043a\u0430 \u0443 \u043d\u0430\u0441 \u043e\u0434\u043d\u0430, \u0442\u043e \u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445.<\/p>\n<h5>\u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0438\u0442\u043e\u0433<\/h5>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0432\u0441\u0435\u0445 \u044d\u0442\u0438\u0445 \u043c\u0430\u043d\u0438\u043f\u0443\u043b\u044f\u0446\u0438\u0439 \u043c\u043d\u0435, \u043a\u0430\u043a \u044f \u043d\u0430\u0434\u0435\u044e\u0441\u044c \u0438 \u0432\u0430\u043c, \u044f\u0441\u043d\u043e \u0447\u0442\u043e \u043f\u0440\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 max() \u043e\u0442\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0447\u0435\u043c dense_rank() \u0432 2 \u0440\u0430\u0437\u0430 \u0438 \u0441\u044a\u0435\u0434\u0430\u0435\u0442 \u0432\u0434\u0432\u043e\u0435 \u043c\u0435\u043d\u044c\u0448\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u041d\u0443 \u043e\u043d\u043e \u0442\u043e \u043f\u043e\u043d\u044f\u0442\u043d\u043e \u0438\u0442\u0430\u043a, \u0431\u0435\u0437 \u043f\u043b\u0430\u043d\u043e\u0432 \u0438 \u043f\u0440\u043e\u0447\u0435\u0433\u043e, \u0432\u0435\u0434\u044c max() \u044d\u0442\u043e \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043f\u043e\u0438\u0441\u043a \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u0433\u043e, \u0432 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a dense_rank() \u2014 \u044d\u0442\u043e, \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430, \u0430 \u0443\u0436\u0435 \u043f\u043e\u0442\u043e\u043c \u043d\u0443\u043c\u0435\u0440\u043e\u0432\u0430\u043d\u0438\u0435.<br \/>  \u041d\u043e \u043d\u0435 \u044d\u0442\u043e \u043f\u043e\u0431\u0443\u0434\u0438\u043b\u043e \u043c\u0435\u043d\u044f \u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0442\u0430\u0442\u044c\u044e.<\/p>\n<h5>\u0412\u043d\u0435\u0437\u0430\u043f\u043d\u043e<\/h5>\n<p>\u0412 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0430, \u044d\u0442\u043e \u044f \u0432\u0435\u0434\u044c \u0434\u043b\u044f \u0441\u0442\u0430\u0442\u044c\u0438 \u0441\u043e\u043e\u0431\u0440\u0430\u0437\u0438\u043b \u0441\u043a\u0440\u0438\u043f\u0442\u044b, \u0430 \u0432 \u043f\u0435\u0440\u0432\u044b\u0439 \u0440\u0430\u0437 \u0432\u0441\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u043b\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432 \u0440\u0443\u0447\u043d\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435, \u0438 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043f\u043e\u0434\u043e\u043f\u044b\u0442\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 <code>order by<\/code>.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">\/*\u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0431\u044b\u043b\u043e 10 \u0438\u043d\u0441\u0435\u0440\u0442\u043e\u0432 \u0438 \u0437\u0430\u043f\u0440\u043e\u0441*\/ insert into habr_test_table_220414...;         ....         .... insert into habr_test_table_220414...; commit; select * from habr_test_table_220414 t order by t.client_id; \/*\u043f\u043e\u0442\u043e\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u043b\u0441\u044f \u043a\u0443\u0441\u043e\u043a:*\/ insert into habr_test_table_220414 select * from habr_test_table_220414; commit; select * from habr_test_table_220414 t order by t.client_id; \/*\u0432\u043e\u0442 \u0442\u0430\u043a \u044f \u0438 \u0441\u0442\u0440\u043e\u0438\u043b \u043f\u0435\u0440\u0432\u044b\u0435 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435*\/ <\/code><\/pre>\n<\/div>\n<\/div>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u044f \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043b \u044d\u0442\u043e\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043e \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u00ab\u0437\u0430\u043f\u0440\u043e\u0441 \u0441 max()\u00bb, \u0442\u0430\u043a \u0438 \u043d\u0435 \u0443\u0431\u0440\u0430\u0432 <code>order by<\/code>. <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0412\u043e\u0442 \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c:<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">select * from (     select c.*         , max(c.oper_id) over (partition by c.client_id) as m_o\/*max_operation*\/     from         (         select t.*         , max(t.amount) over (partition by t.client_id) as m_a\/*max_amount*\/         from habr_test_table_220414 t         order by t.client_id         ) c      where c.m_a = c.amount ) where m_o = oper_id <\/code><\/pre>\n<\/div>\n<\/div>\n<p>\u041f\u043e\u0437\u0436\u0435 \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u00ab\u0437\u0430\u043f\u0440\u043e\u0441 \u0441 dense_rank()\u00bb \u0438 \u043d\u0430\u0447\u0430\u043b \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u043f\u043b\u0430\u043d\u044b, \u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0432 \u044d\u0442\u043e\u0442 \u0437\u043b\u043e\u043f\u043e\u043b\u0443\u0447\u043d\u044b\u0439 <code>order by<\/code> \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u0441 max(), \u0443\u0434\u0430\u043b\u0438\u043b <code>order by<\/code>, \u043d\u043e cost \u0435\u0433\u043e \u044f \u0443\u0436\u0435 \u0443\u0432\u0438\u0434\u0435\u043b \u0438 \u0437\u0430\u043f\u043e\u043c\u043d\u0438\u043b. \u0418 \u043a\u043e\u0433\u0434\u0430 \u0443\u0432\u0438\u0434\u0435\u043b cost \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u0441 max() \u0431\u0435\u0437 <code>order by<\/code> \u0441\u0438\u043b\u044c\u043d\u043e \u0443\u0434\u0438\u0432\u0438\u043b\u0441\u044f, \u0432\u0435\u0434\u044c:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043b\u0430\u043d<\/b><\/p>\n<div class=\"spoiler_text\"><img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/198\/d66\/5b8\/198d665b891a54e8d6a20e6a2c7fe0c0.png\" alt=\"image\"\/><\/div>\n<\/div>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0420\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u043b\u0430\u043d \u043e\u0442 SQLTUNE<\/b><\/p>\n<div class=\"spoiler_text\"><img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/fd4\/56f\/f86\/fd456ff864417a8cc4e0390a8780fd5c.png\" alt=\"image\"\/><\/div>\n<\/div>\n<p> \u0414\u0430 \u0438 \u0432\u043e\u043e\u0431\u0449\u0435, \u0441\u043a\u0430\u0437\u0430\u0442\u044c \u0447\u0442\u043e \u044f \u0441\u0438\u043b\u044c\u043d\u043e \u0443\u0434\u0438\u0432\u0438\u043b\u0441\u044f \u2014 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0441\u043a\u0430\u0437\u0430\u0442\u044c\u2026 \u041a\u0430\u043a \u0442\u0430\u043a \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c? \u041f\u043e\u0447\u0435\u043c\u0443 <code>order by<\/code> \u0443\u0441\u043a\u043e\u0440\u0438\u043b \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 10 \u0440\u0430\u0437? \u0420\u0435\u0448\u0438\u043b \u043d\u0430\u0439\u0442\u0438 \u043e\u0442\u0432\u0435\u0442 \u0432 \u0442\u0440\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u043a\u0435. \u041d\u0435 \u0431\u0443\u0434\u0443 \u043f\u0438\u0441\u0430\u0442\u044c \u043a\u0430\u043a \u0438\u043c\u0435\u043d\u043d\u043e \u0441\u043d\u0438\u043c\u0430\u0442\u044c \u0442\u0440\u0430\u0441\u0441\u0443 \u0432 \u043e\u0440\u0430\u043a\u043b\u0435, \u0438\u0431\u043e \u044d\u0442\u043e \u0442\u0435\u043c\u0430 \u0434\u043b\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438, \u0434\u0430 \u0438 \u0441\u0442\u0430\u0442\u044c\u0438 \u0441 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435\u043c \u0441\u0435\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u043b\u0435\u0433\u043a\u043e \u043d\u0430\u0439\u0442\u0438 \u0432\u043e \u0432\u0441\u0435\u043c\u0438\u0440\u043d\u043e\u0439 \u043f\u0430\u0443\u0442\u0438\u043d\u0435. \u041f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0431\u043e\u0440 \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u044f \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u043b \u0442\u0440\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u043a\u0443 \u0438 \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0442\u0430\u043a\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e, \u043d\u0430\u0448\u0435\u043b \u044f \u0435\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0434\u0430\u0432\u043d\u043e, \u0441 \u0442\u0435\u0445 \u043f\u043e\u0440 \u043e\u043d\u0430 \u043c\u0435\u043d\u044f \u0432\u044b\u0440\u0443\u0447\u0430\u0435\u0442:   <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442<\/b><\/p>\n<div class=\"spoiler_text\"><a href=\"http:\/\/www.sql.ru\/faq\/faq_topic.aspx?fid=389\">\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u0442\u0430\u0442\u044c\u044e \u043f\u043e \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044e \u0442\u0440\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u043a\u0438<\/a><\/p>\n<pre><code class=\"sql\">alter system set timed_statistics=true; alter session set tracefile_identifier='test_for_habr_220414'; alter session set events '10046 trace name context forever, level 12'; select * from (     select c.*         , max(c.oper_id) over (partition by c.client_id) as m_o\/*max_operation*\/     from         (         select t.*         , max(t.amount) over (partition by t.client_id) as m_a\/*max_amount*\/         from habr_test_table_220414 t         order by client_id         ) c      where c.m_a = c.amount ) where m_o = oper_id; alter session set events '10046 trace name context off'; select value from v$parameter p where name='user_dump_dest'; \/*\u0414\u0430\u043b\u0435\u0435 \u0440\u0430\u0441\u0448\u0438\u0444\u0440\u043e\u0432\u044b\u0432\u0430\u0435\u043c \u0444\u0430\u0439\u043b \u0442\u0440\u0430\u0441\u0441\u044b \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 tkprof*\/ \/*\u0432 \u0438\u043c\u0435\u043d\u0438 \u0444\u0430\u0439\u043b\u0430 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c 'test_for_habr_220414'*\/<\/code><\/pre>\n<\/div>\n<\/div>\n<p> \u0412 \u0442\u0440\u0430\u0441\u0441\u0435 \u043d\u0430\u0441 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u0435\u0442 \u043a\u0443\u0441\u043e\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043e\u0440\u0430\u043a\u043b\u0430 \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">select * from (     select c.*         , max(c.oper_id) over (partition by c.client_id) as m_o\/*max_operation*\/     from         (         select t.*         , max(t.amount) over (partition by t.client_id) as m_a\/*max_amount*\/         from habr_test_table_220414 t         order by client_id         ) c     where c.m_a = c.amount ) where m_o = oper_id  call     count       cpu    elapsed       disk      query    current        rows ------- ------  -------- ---------- ---------- ---------- ----------  ---------- Parse       1      0.01       0.00          0          1          0           0 Execute     1      0.00       0.00          0          0          0           0 Fetch       1      0.03       0.02          0         84          0          10 ------- ------  -------- ---------- ---------- ---------- ----------  ---------- total       3      0.04       0.03          0         85          0          10  Misses in library cache during parse: 1 Optimizer mode: ALL_ROWS Parsing user id: SYS  Rows     Row Source Operation -----  ---------------------------------------------------    10 VIEW (cr=84 pr=0 pw=0 time=28155 us cost=23 size=1592850 card=21525)    20 WINDOW BUFFER (cr=84 pr=0 pw=0 time=28145 us cost=23 size=1313025 card=21525)    20  VIEW (cr=84 pr=0 pw=0 time=21628 us cost=23 size=1313025 card=21525) 22010  WINDOW SORT(cr=84 pr=0 pw=0 time=24393 us cost=23 size=1033200 card=21525) 22010  TABLE ACCESS FULL HABR_TEST_TABLE_220414(cr=84 pr=0 pw=0 time=5172 us cost=23 size=1033200 card=21525) <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<h5>\u0418\u0442\u043e\u0433\u0438<\/h5>\n<p>\u041e\u0442\u0441\u044e\u0434\u0430 \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0438 \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439, \u0438 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u043b\u0430\u043d\u044b \u043d\u0435 \u043e\u0448\u0438\u0431\u043e\u0447\u043d\u044b, \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0432\u043e\u0445\u0430 \u0432\u0440\u043e\u0434\u0435 \u0431\u044b \u043a\u0430\u043a \u043d\u0435\u0442 \u0438 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0434\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u0435\u0441\u044f\u0442\u0438\u043a\u0440\u0430\u0442\u043d\u043e\u043c\u0443 \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u044e. \u0410 \u0442\u0430\u043a \u043b\u0438 \u044d\u0442\u043e? <\/p>\n<p>  P.S. \u042f \u0442\u0430\u043a \u0438 \u043d\u0435 \u0441\u043c\u043e\u0433 \u043e\u0442\u0432\u0435\u0442\u0438\u0442\u044c \u043d\u0430 \u044d\u0442\u043e\u0442 \u0432\u043e\u043f\u0440\u043e\u0441 \u0438 \u0432\u0441\u0435 \u0435\u0449\u0435 \u043d\u0435 \u0432\u0435\u0440\u044e \u0432 \u0442\u043e, \u0447\u0442\u043e \u0437\u0430\u043f\u0440\u043e\u0441 \u0440\u0435\u0430\u043b\u044c\u043d\u043e \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u0432 \u0440\u0430\u0437\u044b \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 order by. \u0411\u0443\u0434\u0443 \u0438 \u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u044b\u0442\u0430\u0442\u044c\u0441\u044f \u0432\u044b\u044f\u0441\u043d\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442, \u043a \u0447\u0435\u043c\u0443 \u0438 \u0432\u0430\u0441 \u043f\u0440\u0438\u0437\u044b\u0432\u0430\u044e. \u0418 \u0434\u0430 \u043e\u0442\u043a\u0440\u043e\u044e\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434 \u043d\u0430\u043c\u0438 \u0441\u043e\u043a\u0440\u044b\u0442\u044b\u0435 \u0442\u0430\u0439\u043d\u044b \u043e\u0440\u0430\u043a\u043b\u0430!<br \/>  P.P.S. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044e \u0432\u0441\u0435\u0445 \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435! \u0415\u0441\u043b\u0438 \u0432\u044b \u0432\u043c\u0435\u0441\u0442\u0435 \u0441\u043e \u043c\u043d\u043e\u0439 \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u043b\u0438 \u0442\u0435\u0441\u0442 \u2014 \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0437\u0430 \u0441\u043e\u0431\u043e\u0439 \u0431\u0430\u0437\u0443, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043f\u0440\u043e\u0434 \u043a\u0430\u043a\u043e\u0433\u043e \u043d\u0438\u0431\u0443\u0434\u044c \u0431\u0430\u043d\u043a\u0430.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">drop trigger    habr_test_trigger_220414; drop sequence   habr_test_sequence_220414; drop table      habr_test_table_220414;<\/code><\/pre>\n<\/div>\n<\/div>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/220455\/\"> http:\/\/habrahabr.ru\/post\/220455\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\"> \t\t\t\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435, \u043a\u043e\u043b\u043b\u0435\u0433\u0438.<br \/>  \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e \u0441\u0432\u043e\u0438\u0445 \u0438\u0437\u044b\u0441\u043a\u0430\u043d\u0438\u044f\u0445 \u0432 \u0432\u043e\u043f\u0440\u043e\u0441\u0435: \u00ab\u0410 \u0447\u0442\u043e \u0436\u0435 \u043b\u0443\u0447\u0448\u0435: dense_rank() \u0438\u043b\u0438 max()\u00bb \u0438, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u043f\u043e\u0447\u0435\u043c\u0443 \u044d\u0442\u0438 \u0438\u0437\u044b\u0441\u043a\u0430\u043d\u0438\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u0438\u0441\u044c \u0441 \u043d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u044b\u043c, \u043f\u043e \u043a\u0440\u0430\u0439\u043d\u0435\u0439 \u043c\u0435\u0440\u0435 \u0434\u043b\u044f \u043c\u0435\u043d\u044f, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-220455","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/220455","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=220455"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/220455\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=220455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=220455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=220455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}