{"id":3111,"date":"2019-04-16T22:21:23","date_gmt":"2019-04-16T20:21:23","guid":{"rendered":"http:\/\/van-maanen.com\/?p=3111"},"modified":"2019-04-16T22:21:23","modified_gmt":"2019-04-16T20:21:23","slug":"take-latest-values","status":"publish","type":"post","link":"http:\/\/archief.van-maanen.com\/?p=3111","title":{"rendered":"Take latest values"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The situation is as follows. We have a table with records that are added over time. The request is to produce a resultset that only contains values that are added latest.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let me first show the table:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CREATE TABLE \"RELATION_VALUES\" <br>\n   (    \"RELATION_ID\" NUMBER, <br>\n    \"STARTDATE\" DATE, <br>\n    \"VALUE\" NUMBER<br>\n   ) ;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Insert into RELATION_VALUES (RELATION_ID,STARTDATE,VALUE) values ('1',to_date('11-FEB-2019','DD-MON-YYYY'),'80');<br>\nInsert into RELATION_VALUES (RELATION_ID,STARTDATE,VALUE) values ('2',to_date('12-FEB-2019','DD-MON-YYYY'),'60');<br>\nInsert into RELATION_VALUES (RELATION_ID,STARTDATE,VALUE) values ('2',to_date('14-FEB-2019','DD-MON-YYYY'),'70');<br>\nInsert into RELATION_VALUES (RELATION_ID,STARTDATE,VALUE) values ('1',to_date('14-FEB-2019','DD-MON-YYYY'),'50');<br>\nInsert into RELATION_VALUES (RELATION_ID,STARTDATE,VALUE) values ('2',to_date('15-FEB-2019','DD-MON-YYYY'),'80');<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For relation_id =1 we see that over time the values 80, 50 are added.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Similarly, for relation_id=2, we see that over time the values 60, 70, 80 are added.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We would like to see a report that only contains the latest values. That implies we would like to see 50 and 80 only for relation_id 1 and 2 respectively.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The query to use is:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">select <br>\n    relation_id<br>\n    , max(value) KEEP(DENSE_RANK last ORDER BY \"STARTDATE\" ASC)  as value <br>\nfrom relation_values <br>\ngroup by relation_id;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The result is: <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/van-maanen.com\/wp-content\/uploads\/2019\/04\/Untitled-3-2-1-1.png\" alt=\"\" class=\"wp-image-3116\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>The situation is as follows. We have a table with records that are added over time. The request is to produce a resultset that only contains values that are added latest. Let me first show the table: CREATE TABLE &#8220;RELATION_VALUES&#8221; ( &#8220;RELATION_ID&#8221; NUMBER, &#8220;STARTDATE&#8221; DATE, &#8220;VALUE&#8221; NUMBER ) ; Insert into RELATION_VALUES (RELATION_ID,STARTDATE,VALUE) values (&#8216;1&#8242;,to_date(&#8217;11-FEB-2019&#8242;,&#8217;DD-MON-YYYY&#8217;),&#8217;80&#8217;); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-3111","post","type-post","status-publish","format-standard","hentry","category-nice-to-know"],"_links":{"self":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3111"}],"version-history":[{"count":0,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3111\/revisions"}],"wp:attachment":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3111"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}