{"id":682,"date":"2024-07-05T11:41:09","date_gmt":"2024-07-05T03:41:09","guid":{"rendered":"https:\/\/info.juliusgoh.life\/?p=682"},"modified":"2024-07-05T11:42:35","modified_gmt":"2024-07-05T03:42:35","slug":"mysql-row_number-usage-and-example","status":"publish","type":"post","link":"https:\/\/info.juliusgoh.life\/?p=682","title":{"rendered":"MYSQL Row_number usage and example"},"content":{"rendered":"\n<p>Imagine data as below:-<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>category_id<\/th><th>item_name<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Item A1<\/td><\/tr><tr><td>1<\/td><td>Item A2<\/td><\/tr><tr><td>1<\/td><td>Item A2<\/td><\/tr><tr><td>2<\/td><td>Item B1<\/td><\/tr><tr><td>2<\/td><td>Item B1<\/td><\/tr><tr><td>2<\/td><td>Item B2<\/td><\/tr><tr><td>3<\/td><td>Item C1<\/td><\/tr><tr><td>3<\/td><td>Item C2<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    category_id, \n    item_name,\n    ROW_NUMBER() OVER(PARTITION BY category_id, item_name ORDER BY category_id, item_name) AS displayOrder\nFROM \n    items\nORDER BY \n    category_id, displayOrder;<\/code><\/pre>\n\n\n\n<p>In this query:<\/p>\n\n\n\n<ul>\n<li><strong>ROW_NUMBER() OVER(PARTITION BY category_id, item_name ORDER BY category_id, item_name)<\/strong>: The <code>PARTITION BY<\/code> clause partitions the result set by both <code>category_id<\/code> and <code>item_name<\/code>. <code>ROW_NUMBER()<\/code> then assigns a sequential number (<code>displayOrder<\/code>) starting from 1 within each partition, ordered first by <code>category_id<\/code> and then by <code>item_name<\/code>.<\/li>\n\n\n\n<li><strong>SELECT category_id, item_name<\/strong>: Selects only <code>category_id<\/code> and <code>item_name<\/code> from the <code>items<\/code> table.<\/li>\n\n\n\n<li><strong>ORDER BY category_id, displayOrder<\/strong>: Orders the result set first by <code>category_id<\/code> and then by <code>displayOrder<\/code>, ensuring that items within each <code>category_id<\/code> and <code>item_name<\/code> group are ordered sequentially based on <code>category_id<\/code> and <code>item_name<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>Now, let&#8217;s see the result of executing this modified query on our sample data:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>category_id<\/th><th>item_name<\/th><th>displayOrder<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Item A1<\/td><td>1<\/td><\/tr><tr><td>1<\/td><td>Item A2<\/td><td>2<\/td><\/tr><tr><td>1<\/td><td>Item A2<\/td><td>3<\/td><\/tr><tr><td>2<\/td><td>Item B1<\/td><td>1<\/td><\/tr><tr><td>2<\/td><td>Item B1<\/td><td>2<\/td><\/tr><tr><td>2<\/td><td>Item B2<\/td><td>3<\/td><\/tr><tr><td>3<\/td><td>Item C1<\/td><td>1<\/td><\/tr><tr><td>3<\/td><td>Item C2<\/td><td>2<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>In this result:<\/p>\n\n\n\n<ul>\n<li>The <code>item_id<\/code> is not included.<\/li>\n\n\n\n<li><code>displayOrder<\/code> starts from 1 within each combination of <code>category_id<\/code> and <code>item_name<\/code>, ordered first by <code>category_id<\/code> and then by <code>item_name<\/code>.<\/li>\n\n\n\n<li>Items are ordered first by <code>category_id<\/code>, and within each <code>category_id<\/code>, items with the same <code>item_name<\/code> are ordered by <code>item_name<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>This query structure maintains the sequential ordering (<code>displayOrder<\/code>) within each unique combination of <code>category_id<\/code> and <code>item_name<\/code>, ensuring the rows are ordered by both criteria within their respective partitions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Imagine data as below:- category_id item_name 1 Item A1 1 Item A2 1 Item A2 2 Item B1 2 Item B1 2 Item B2 3 Item C1 3 Item C2 In this query: Now, let&#8217;s see the result of executing this modified query on our sample data: category_id item_name displayOrder 1 Item A1 1 1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":217,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"image","meta":{"footnotes":""},"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts\/682"}],"collection":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=682"}],"version-history":[{"count":3,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts\/682\/revisions"}],"predecessor-version":[{"id":704,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/posts\/682\/revisions\/704"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=\/wp\/v2\/media\/217"}],"wp:attachment":[{"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/info.juliusgoh.life\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}