{"id":21319,"date":"2022-11-13T01:42:34","date_gmt":"2022-11-12T20:12:34","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/"},"modified":"2022-11-13T01:42:34","modified_gmt":"2022-11-12T20:12:34","slug":"solved-show-each-row-that-is-a-duplicate","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/","title":{"rendered":"[Solved] Show each row that is a duplicate"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-30183784\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"30183784\" data-parentid=\"30182836\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<h2>Final update<\/h2>\n<p>After yet another goal post shifting, I&#8217;ve updated my CTE again.<br \/>\nThis is the final update since even if you are going to change your demands again I&#8217;ve had enough.<br \/>\nPlease take my advice for future questions:<\/p>\n<ul>\n<li>Defind the problem as well as you can.<\/li>\n<li>Provide the most accurate table structure and sample data as ddl+dml.<br \/>\nDon&#8217;t link to sqlfiddle as it suffers from a lot of downtime.<\/li>\n<li>Provide the most accurate expected output<\/li>\n<li>Show your efforts to solve the problem.<\/li>\n<\/ul>\n<p>Here is the last updated CTE, everything else remains as it was in the last update. <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/sqlfiddle.com\/#!6\/c96cc\/5\">sqlfiddle here.<\/a><\/p>\n<pre><code>;WITH CTE AS (\n\nSELECT OriginalUrl, \n       NewUrl,\n       RIGHT(OriginalUrl, \n             LEN(OriginalUrl) - \n                 CHARINDEX(\"https:\/\/stackoverflow.com\/\", OriginalUrl,\n                     CASE WHEN PATINDEX('%:\/\/%\/%', OriginalUrl) &gt; 0 THEN\n                         PATINDEX('%:\/\/%\/%', OriginalUrl)+3\n                     ELSE\n                        0\n                     END\n                 )+1\n             ) As Product\nFROM MyTable \n)\n<\/code><\/pre>\n<h2>Update<\/h2>\n<p>Updated sample data (again&#8230;.)<\/p>\n<pre><code>-- Create sample table and data. \n-- DDL\ncreate table MyTable (\n  OriginalUrl varchar(50),\n  NewUrl varchar(50)\n)\n-- DML\n  insert into MyTable VALUES \n('\/blog', '\/blog'),\n('http:\/\/gaming.corsair.com\/blog', 'http:\/\/gaming.corsair.com\/blog'),\n('http:\/\/www.corsair.com\/blog', 'http:\/\/www.corsair.com\/blog'),\n('http:\/\/www.corsair.com\/es\/blog', 'http:\/\/www.corsair.com\/es\/blog')\n<\/code><\/pre>\n<p>I&#8217;ve updated the CTE to include in &#8216;Product&#8217; everything from the last &#8220;https:\/\/stackoverflow.com\/&#8221; char, so product for the first 3 rows is now <code>\/blog<\/code>, and for the last one it&#8217;s <code>\/es\/blog<\/code>. so everything after the protocol and domain parts of the URL is considered a product now. Note &#8211; this also should apply to <code>https<\/code> and any other protocol.<\/p>\n<pre><code>;WITH CTE AS (\n\nSELECT OriginalUrl, \n       NewUrl,\n       RIGHT(OriginalUrl, \n             LEN(OriginalUrl) - \n                 CHARINDEX(\"https:\/\/stackoverflow.com\/\", OriginalUrl,\n                     PATINDEX('%:\/\/%', OriginalUrl)+3 -- get the location right after ':\/\/' \n                 )+1 -- get the location right after the first \/ after the patindex\n             ) As Product\nFROM MyTable \n)\n<\/code><\/pre>\n<p>My query on the new CTE remain as it was in the last attempt:<\/p>\n<pre><code>SELECT T1.OriginalUrl, T1.NewUrl,T2.Product \nFROM CTE T1 \nINNER JOIN (\n    SELECT Product\n    FROM CTE\n    GROUP BY Product \n    HAVING COUNT(1) &gt; 1\n) T2 ON(T1.Product = T2.Product)\n<\/code><\/pre>\n<p>Here is the output:<\/p>\n<pre><code>OriginalUrl                       NewUrl                            Product\n---------------------------------------------------------------------------\n\/blog                             \/blog                             \/blog\nhttp:\/\/gaming.corsair.com\/blog    http:\/\/gaming.corsair.com\/blog    \/blog\nhttp:\/\/www.corsair.com\/blog       http:\/\/www.corsair.com\/blog       \/blog\n<\/code><\/pre>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/sqlfiddle.com\/#!6\/a4510\/6\">You can play with it yourself on sqlFiddle.<\/a><\/p>\n<h2>Earlier versions<\/h2>\n<p>So, by duplicates you mean all rows that have the same string after the last <code>\/<\/code> in OriginalUrl column?<br \/>\nIf so, you might want to try this:<\/p>\n<pre><code>-- Create sample table and data. \n-- DDL\ncreate table MyTable (\n  OriginalUrl varchar(50),\n  NewUrl varchar(50)\n)\n-- DML\n  insert into MyTable VALUES \n('\/blog', '\/en-us\/blog'),\n('\/blog', '\/en-us\/blog'),\n('http:\/\/www.corsair.com\/blog', 'http:\/\/www.corsair.com\/blog'),\n('http:\/\/gaming.corsair.com\/blog', 'http:\/\/gaming.corsair.com\/blog'),\n('blablabla\/blog', 'yadayada\/blog'),\n('I don''t see what is wrong with this\/Answer', 'It seems to be working\/Fine'),\n('Unless my\/Answer', 'assumes duplicates as something else then\/you'),\n('300r', '300r')\n<\/code><\/pre>\n<p><strong>Note:<\/strong> You should use this way to provide sample data when asking questions about sql.<br \/>\nThis way we can copy your DDL + DML to sqlfiddle or our own environments and actually test the answers we give.<\/p>\n<p>I&#8217;ve Used a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/technet.microsoft.com\/en-us\/library\/ms190766%28v=sql.105%29.aspx\">CTE<\/a> to insulate the last word after the last \/ in your sample data,<br \/>\nso that I will only have to write the RIGHT expression once.<\/p>\n<pre><code>;WITH CTE AS (\nSELECT OriginalUrl, \n        NewUrl,\n        RIGHT(OriginalUrl, CASE WHEN CHARINDEX(\"https:\/\/stackoverflow.com\/\", OriginalUrl) &gt; 0 THEN\n                               CHARINDEX(\"https:\/\/stackoverflow.com\/\", REVERSE(OriginalUrl))-1\n                           ELSE \n                               LEN(OriginalUrl)\n                           END) As Product\nFROM MyTable \n)\n\nSELECT DISTINCT T1.OriginalUrl, T1.NewUrl,T1.Product \nFROM CTE T1 \nINNER JOIN CTE T2\nON(T1.Product = T2.Product)\nWHERE T1.OriginalUrl &lt;&gt; T2.OriginalUrl \n<\/code><\/pre>\n<p><strong>Update<\/strong><br \/>\nUsing the same CTE, try this:<\/p>\n<pre><code>SELECT T1.OriginalUrl, T1.NewUrl,T2.Product \nFROM CTE T1 \nINNER JOIN (\n    SELECT Product\n    FROM CTE\n    GROUP BY Product \n    HAVING COUNT(1) &gt; 1\n) T2 ON(T1.Product = T2.Product)\n<\/code><\/pre>\n<p>Results:<\/p>\n<pre><code>    OriginalUrl                                        NewUrl                                             Product\n    -------------------------------------------------- -------------------------------------------------- --------------------------------------------------\n    I don't see what is wrong with this\/Answer         It seems to be working\/Fine                        Answer\n    Unless my\/Answer                                   assumes duplicates as something else then\/you      Answer\n    \/blog                                              \/en-us\/blog                                        blog\n    \/blog                                              \/en-us\/blog                                        blog\n    http:\/\/www.corsair.com\/blog                        http:\/\/www.corsair.com\/blog                        blog\n    http:\/\/gaming.corsair.com\/blog                     http:\/\/gaming.corsair.com\/blog                     blog\n    blablabla\/blog                                     yadayada\/blog                                      blog\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">10<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Show each row that is a duplicate <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Final update After yet another goal post shifting, I&#8217;ve updated my CTE again. This is the final update since even if you are going to change your demands again I&#8217;ve had enough. Please take my advice for future questions: Defind the problem as well as you can. Provide the most accurate table structure and &#8230; <a title=\"[Solved] Show each row that is a duplicate\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/\" aria-label=\"More on [Solved] Show each row that is a duplicate\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[500],"class_list":["post-21319","post","type-post","status-publish","format-standard","hentry","category-solved","tag-sql-server"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Show each row that is a duplicate - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Show each row that is a duplicate - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Final update After yet another goal post shifting, I&#8217;ve updated my CTE again. This is the final update since even if you are going to change your demands again I&#8217;ve had enough. Please take my advice for future questions: Defind the problem as well as you can. Provide the most accurate table structure and ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-12T20:12:34+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Show each row that is a duplicate\",\"datePublished\":\"2022-11-12T20:12:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/\"},\"wordCount\":314,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"sql-server\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/\",\"name\":\"[Solved] Show each row that is a duplicate - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-12T20:12:34+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Show each row that is a duplicate\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Show each row that is a duplicate - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Show each row that is a duplicate - JassWeb","og_description":"[ad_1] Final update After yet another goal post shifting, I&#8217;ve updated my CTE again. This is the final update since even if you are going to change your demands again I&#8217;ve had enough. Please take my advice for future questions: Defind the problem as well as you can. Provide the most accurate table structure and ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/","og_site_name":"JassWeb","article_published_time":"2022-11-12T20:12:34+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Show each row that is a duplicate","datePublished":"2022-11-12T20:12:34+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/"},"wordCount":314,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["sql-server"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/","url":"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/","name":"[Solved] Show each row that is a duplicate - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-12T20:12:34+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-show-each-row-that-is-a-duplicate\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Show each row that is a duplicate"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/21319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=21319"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/21319\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=21319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=21319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=21319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}