{"id":504,"date":"2013-11-28T19:05:57","date_gmt":"2013-11-29T00:05:57","guid":{"rendered":"http:\/\/binaryworld.net\/blogs\/?p=504"},"modified":"2014-02-27T09:47:24","modified_gmt":"2014-02-27T14:47:24","slug":"how-to-exclude-duplicate-records","status":"publish","type":"post","link":"https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/","title":{"rendered":"How to exclude duplicate records"},"content":{"rendered":"<p>Here is simple query which shows simple technique to exclude duplicate records from output. In my example I am selecting only first record from duplicate list based on Primary key.<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\nSelect * into #tmp1\r\nFrom(\r\nselect 1 as id,&#039;aaa&#039; as name,11 as age \r\nUNION ALL\r\nselect 1 as id,&#039;aaa&#039; as name,12 as age \r\nUNION ALL\r\nselect 2 as id,&#039;bbb&#039; as name,22 as age \r\nUNION ALL\r\nselect 2 as id,&#039;bbb&#039; as name,23 as age \r\nUNION ALL\r\nselect 3 as id,&#039;ccc&#039; as name,33 as age \r\n) a\r\n\r\n\r\nselect a.*,b.* \r\nfrom #tmp1 a \r\nLEFT JOIN \r\n(   \r\n   SELECT \r\n\t   ROW_NUMBER() OVER(PARTITION BY id ORDER BY id,name) AS Row\r\n\t  ,id,name,age\r\n   FROM \r\n   ( \r\n\t  SELECT id ,name,age\r\n\t  FROM #tmp1\r\n\t  where id IN ( SELECT id FROM #tmp1 group by id having count(*)&gt;1 ) \r\n   ) all_dups \r\n) b --\/\/dups with counter\r\nON a.id=b.id and a.name=b.name and a.age=b.age --\/\/todo add many more ....\r\nWhere b.Row IS NULL or b.Row=1\r\n\r\ngo\r\n\r\ndrop table #tmp1\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is simple query which shows simple technique to exclude duplicate records from output. In my example I am selecting only first record from duplicate list based on Primary key. Select * into #tmp1 From( select 1 as id,&#039;aaa&#039; as &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/\"> <span class=\"screen-reader-text\">How to exclude duplicate records<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":606,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,35],"tags":[158,85],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\r\n<title>How to exclude duplicate records - BinaryWorld Blog<\/title>\r\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/\" \/>\r\n<meta property=\"og:locale\" content=\"en_US\" \/>\r\n<meta property=\"og:type\" content=\"article\" \/>\r\n<meta property=\"og:title\" content=\"How to exclude duplicate records - BinaryWorld Blog\" \/>\r\n<meta property=\"og:description\" content=\"Here is simple query which shows simple technique to exclude duplicate records from output. In my example I am selecting only first record from duplicate list based on Primary key. Select * into #tmp1 From( select 1 as id,&#039;aaa&#039; as &hellip; How to exclude duplicate records Read More &raquo;\" \/>\r\n<meta property=\"og:url\" content=\"https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/\" \/>\r\n<meta property=\"og:site_name\" content=\"BinaryWorld Blog\" \/>\r\n<meta property=\"article:published_time\" content=\"2013-11-29T00:05:57+00:00\" \/>\r\n<meta property=\"article:modified_time\" content=\"2014-02-27T14:47:24+00:00\" \/>\r\n<meta property=\"og:image\" content=\"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2014\/02\/sql-server-logo-e1397590261883.jpg\" \/>\r\n\t<meta property=\"og:image:width\" content=\"150\" \/>\r\n\t<meta property=\"og:image:height\" content=\"150\" \/>\r\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\r\n<meta name=\"author\" content=\"Binary World\" \/>\r\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\r\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Binary World\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\r\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/\",\"url\":\"https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/\",\"name\":\"How to exclude duplicate records - BinaryWorld Blog\",\"isPartOf\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2014\/02\/sql-server-logo-e1397590261883.jpg\",\"datePublished\":\"2013-11-29T00:05:57+00:00\",\"dateModified\":\"2014-02-27T14:47:24+00:00\",\"author\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/77cf0a9a512dd22bff93c6a1b6374fe0\"},\"breadcrumb\":{\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/#primaryimage\",\"url\":\"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2014\/02\/sql-server-logo-e1397590261883.jpg\",\"contentUrl\":\"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2014\/02\/sql-server-logo-e1397590261883.jpg\",\"width\":150,\"height\":150,\"caption\":\"T SQL Date format convert function\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/binaryworld.net\/blogs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to exclude duplicate records\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/#website\",\"url\":\"https:\/\/binaryworld.net\/blogs\/\",\"name\":\"BinaryWorld Blog\",\"description\":\"Tips and Tutorials for Microsoft SQL Server, SSIS, SSAS, Business Intelligence, C#, .net\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/binaryworld.net\/blogs\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/77cf0a9a512dd22bff93c6a1b6374fe0\",\"name\":\"Binary World\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/eaea47799daa577835eb53e64dfd3e13?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/eaea47799daa577835eb53e64dfd3e13?s=96&d=mm&r=g\",\"caption\":\"Binary World\"},\"description\":\"Binary World is a Software Development company located in Atlanta, USA (since 2007). Binary World specialized in Business Intelligence, mobile, cloud computing and .Net Application Development.\",\"url\":\"https:\/\/binaryworld.net\/blogs\/author\/admin\/\"}]}<\/script>\r\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to exclude duplicate records - BinaryWorld Blog","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:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/","og_locale":"en_US","og_type":"article","og_title":"How to exclude duplicate records - BinaryWorld Blog","og_description":"Here is simple query which shows simple technique to exclude duplicate records from output. In my example I am selecting only first record from duplicate list based on Primary key. Select * into #tmp1 From( select 1 as id,&#039;aaa&#039; as &hellip; How to exclude duplicate records Read More &raquo;","og_url":"https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/","og_site_name":"BinaryWorld Blog","article_published_time":"2013-11-29T00:05:57+00:00","article_modified_time":"2014-02-27T14:47:24+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2014\/02\/sql-server-logo-e1397590261883.jpg","type":"image\/jpeg"}],"author":"Binary World","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Binary World","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/","url":"https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/","name":"How to exclude duplicate records - BinaryWorld Blog","isPartOf":{"@id":"https:\/\/binaryworld.net\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/#primaryimage"},"image":{"@id":"https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/#primaryimage"},"thumbnailUrl":"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2014\/02\/sql-server-logo-e1397590261883.jpg","datePublished":"2013-11-29T00:05:57+00:00","dateModified":"2014-02-27T14:47:24+00:00","author":{"@id":"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/77cf0a9a512dd22bff93c6a1b6374fe0"},"breadcrumb":{"@id":"https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/#primaryimage","url":"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2014\/02\/sql-server-logo-e1397590261883.jpg","contentUrl":"https:\/\/binaryworld.net\/blogs\/wp-content\/uploads\/2014\/02\/sql-server-logo-e1397590261883.jpg","width":150,"height":150,"caption":"T SQL Date format convert function"},{"@type":"BreadcrumbList","@id":"https:\/\/binaryworld.net\/blogs\/how-to-exclude-duplicate-records\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/binaryworld.net\/blogs\/"},{"@type":"ListItem","position":2,"name":"How to exclude duplicate records"}]},{"@type":"WebSite","@id":"https:\/\/binaryworld.net\/blogs\/#website","url":"https:\/\/binaryworld.net\/blogs\/","name":"BinaryWorld Blog","description":"Tips and Tutorials for Microsoft SQL Server, SSIS, SSAS, Business Intelligence, C#, .net","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/binaryworld.net\/blogs\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/77cf0a9a512dd22bff93c6a1b6374fe0","name":"Binary World","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/binaryworld.net\/blogs\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/eaea47799daa577835eb53e64dfd3e13?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/eaea47799daa577835eb53e64dfd3e13?s=96&d=mm&r=g","caption":"Binary World"},"description":"Binary World is a Software Development company located in Atlanta, USA (since 2007). Binary World specialized in Business Intelligence, mobile, cloud computing and .Net Application Development.","url":"https:\/\/binaryworld.net\/blogs\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/posts\/504"}],"collection":[{"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/comments?post=504"}],"version-history":[{"count":0,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/posts\/504\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/media\/606"}],"wp:attachment":[{"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/media?parent=504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/categories?post=504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/binaryworld.net\/blogs\/wp-json\/wp\/v2\/tags?post=504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}