{"id":3147,"date":"2020-02-26T11:56:15","date_gmt":"2020-02-26T10:56:15","guid":{"rendered":"http:\/\/www.capri-soft.de\/blog\/?p=3147"},"modified":"2020-02-26T12:34:45","modified_gmt":"2020-02-26T11:34:45","slug":"microsoft-sql-server-how-to-realize-a-sql-statement-that-only-returns-the-maximum-or-the-minimum-rows-of-a-group-similar-to-an-non-existing-aggregate-function-for-the-group-by-clause","status":"publish","type":"post","link":"https:\/\/www.capri-soft.de\/blog\/?p=3147","title":{"rendered":"Microsoft SQL Server: How-to realize a SQL Statement that  only returns the maximum or the minimum rows of a group (similar to an non-existing aggregate function for the GROUP BY clause)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Actual situation<\/h2>\n\n\n\n<p>A change request (field &#8222;pcr&#8220;) only can have one unique assigned user that shall be informed about the assignment every night.<br><br>The assigned user (field &#8222;created_by&#8220;) of a change requests (field &#8222;pcr&#8220;) is changing several times a day. Every time the user (field &#8222;created_by&#8220;) changes to another user, a table entry with the actual time stamp (field &#8222;created_on&#8220;) is inserted to a SQL table.<\/p>\n\n\n\n<p>At the end of the day, the system shall report via email the users that have been assigned to a PCR (only one user per change request). <br><br>The following table contains entries with people wo shall be informed every evening.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"\"><thead><tr><th>id<\/th><th>pcr<\/th><th>created_on<\/th><th>created_by<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>PCR-1<\/td><td>2020-02-26 09:20:52.977<\/td><td>user1<\/td><\/tr><tr><td>2<\/td><td>PCR-1<\/td><td>2020-02-26 09:20:54.667<\/td><td>user1<\/td><\/tr><tr><td>3<\/td><td>PCR-2<\/td><td>2020-02-26 09:20:57.137<\/td><td>user1<\/td><\/tr><tr><td>4<\/td><td>PCR-2<\/td><td>2020-02-26 09:20:59.003<\/td><td>user1<\/td><\/tr><tr><td>5<\/td><td>PCR-2<\/td><td>2020-02-26 09:21:07.540<\/td><td>user3<\/td><\/tr><tr><td>6<\/td><td>PCR-3<\/td><td>2020-02-26 09:21:09.850<\/td><td>user3<\/td><\/tr><tr><td>7<\/td><td>PCR-4<\/td><td>2020-02-26 09:21:12.850<\/td><td>user3<\/td><\/tr><tr><td>8<\/td><td>PCR-4<\/td><td>2020-02-26 09:21:15.100<\/td><td>user3<\/td><\/tr><tr><td>9<\/td><td>PCR-5<\/td><td>2020-02-26 09:21:55.323 <\/td><td>user3<\/td><\/tr><tr><td>10<\/td><td>PCR-2<\/td><td>2020-02-26 09:22:06.037<\/td><td>user2<\/td><\/tr><tr><td>11<\/td><td>PCR-5<\/td><td>2020-02-26 09:22:07.037<\/td><td>user2<\/td><\/tr><tr><td>12<\/td><td>PCR-7<\/td><td>2020-02-26 09:22:08.760    <\/td><td>user2<\/td><\/tr><tr><td>13<\/td><td>PCR-7<\/td><td>2020-02-26 10:20:07.233<\/td><td>user2<\/td><\/tr><tr><td>14<\/td><td>PCR-8<\/td><td>2020-02-26 10:20:08.967<\/td><td>user2<\/td><\/tr><tr><td>15<\/td><td>PCR-8<\/td><td>2020-02-26 10:20:13.393<\/td><td>user1<\/td><\/tr><tr><td>16<\/td><td>PCR-9<\/td><td>2020-02-26 10:20:15.020<\/td><td>user1<\/td><\/tr><tr><td>17<\/td><td>PCR-7<\/td><td>2020-02-26 10:20:19.497<\/td><td>user3<\/td><\/tr><tr><td>18<\/td><td>PCR-9<\/td><td>2020-02-26 10:20:20.163<\/td><td>user3<\/td><\/tr><tr><td><\/td><td><\/td><td><\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>A nightly executed batch job (at 23:59h every evening) shall provide the possibility to inform user1, user2 or user3 about their assigned PCR&#8217;s. Only the latest entry per PCR, that can be recognized by the timestamp (field &#8222;created_on&#8220;) represent the user assigned user at the end of the day<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Problem<\/h2>\n\n\n\n<p>The SQL Statement shall output the latest entry per assigned user of each PCR. With a standard aggregate function this is not possible because the whole row entry has to be retrieved.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Approach<\/h2>\n\n\n\n<p>The Microsoft SQL Server has a proprietary function RANK(), that prints out the ranking of the PCR entry in connection with PARTITION BY and ORDER BY. The ORDER BY has to be used DESC, so that the latest entry always is on rank 1. <br><br>Because this methods only can be used within the SELECT statements fields and not withing WHERE, we need a second parent query to select everything with rank 1.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Solution<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT *\nFROM (\n\tSELECT\tid, \n\t\t\tpcr,\n\t\t\tcreated_on, \n\t\t\tcreated_by,\n\t\t\tRANK () OVER ( \n\t\t\t\tPARTITION BY pcr\n\t\t\t\tORDER BY created_on DESC\n\t\t\t) as biggestHasRangOne\n\tFROM nightly_email\n)tab \nWHERE biggestHasRangOne=1 \nAND CONVERT(date, GETDATE())=CONVERT (date, created_on)\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"440\" height=\"174\" data-attachment-id=\"3151\" data-permalink=\"https:\/\/www.capri-soft.de\/blog\/?attachment_id=3151\" data-orig-file=\"https:\/\/i0.wp.com\/www.capri-soft.de\/blog\/wp-content\/uploads\/2020\/02\/image-12.png?fit=440%2C174&amp;ssl=1\" data-orig-size=\"440,174\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-12\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.capri-soft.de\/blog\/wp-content\/uploads\/2020\/02\/image-12.png?fit=440%2C174&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.capri-soft.de\/blog\/wp-content\/uploads\/2020\/02\/image-12.png?resize=440%2C174&#038;ssl=1\" alt=\"\" class=\"wp-image-3151\" srcset=\"https:\/\/i0.wp.com\/www.capri-soft.de\/blog\/wp-content\/uploads\/2020\/02\/image-12.png?w=440&amp;ssl=1 440w, https:\/\/i0.wp.com\/www.capri-soft.de\/blog\/wp-content\/uploads\/2020\/02\/image-12.png?resize=300%2C119&amp;ssl=1 300w\" sizes=\"auto, (max-width: 440px) 100vw, 440px\" \/><figcaption>Only the latest PCR assignment has been selected for that day.<\/figcaption><\/figure><\/div>\n\n\n\n<p>To get the smallest one (minimum) in for that day you have to use<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ORDER BY created_on ASC<\/pre>\n\n\n\n<p>In the RANK() &#8211; Partition by -Syntax.<\/p>\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=https%3A%2F%2Fwww.capri-soft.de%2Fblog%2F%3Fp%3D3147&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px;margin-top:5px;\"><\/iframe>","protected":false},"excerpt":{"rendered":"<p>Actual situation A change request (field &#8222;pcr&#8220;) only can have one unique assigned user that shall be informed about the assignment every night. The assigned user (field &#8222;created_by&#8220;) of a change requests (field &#8222;pcr&#8220;) is changing several times a day. Every time the user (field &#8222;created_by&#8220;) changes to another user, a table entry with the &hellip; <a href=\"https:\/\/www.capri-soft.de\/blog\/?p=3147\" class=\"more-link\"><span class=\"screen-reader-text\">Microsoft SQL Server: How-to realize a SQL Statement that  only returns the maximum or the minimum rows of a group (similar to an non-existing aggregate function for the GROUP BY clause)<\/span> weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[11,3,43],"tags":[98,102,99,100,97,101],"class_list":["post-3147","post","type-post","status-publish","format-standard","hentry","category-ms-sql-server","category-programmierung","category-sql","tag-aggregate","tag-microsoft-sql-server","tag-partition-by","tag-rank","tag-sql","tag-tsql"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4yGeN-OL","jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3147"}],"version-history":[{"count":4,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3147\/revisions"}],"predecessor-version":[{"id":3154,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3147\/revisions\/3154"}],"wp:attachment":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}