Skip to main content

How to use CTE to remove duplicates



--One use of Common table Expressions (CTEs) is to remove duplicates
--To see this lets create a table 'Employee'
CREATE table Employee  (name NVARCHAR(100), age INT, salary INT)

--insert some duplicate data into the 'Employee Table'.
INSERT  into Employee
VALUES ('Mark', 42, 120000), ('Susan', 29, 125000), ('Mark', 42, 120000)
--you could see there is a duplicate record in the table by querying the table
--select * from  Employee
--we could use CTE to remove this duplicate. 
WITH CTE_Employee(name, age, salary, rownumber) AS (
select name,   age,   salary,   row_number() OVER (
PARTITION BY name, age, salary ORDER BY name, age, salary
) AS rownumber from Employee
)

DELETE
FROM CTE_Employee
WHERE rownumber <> 1
--query the table to see if there are still duplicates
--select * from  Employee

Comments

Popular posts from this blog

UML Use Case Diagram Tutorial

Data Warehouse info   Data Warehouse   OLTP vs. OLAP   What is Business Intelligence   Business Intelligence tools   ETL tools   ETL process   Magic Quadrant for Business Intelligence   Magic Quadrant for Data Integration Tools   DW Database Management Systems   BI market consolidation   Data Warehouse Schema Architecture   Slowly Changing Dimensions (SCD)   Magic Quadrant for Data Quality Tools   Business Intelligence Platforms   Teradata Certification News   News   Archive Recently added reviews   Talend - Talend Open Studio   Teradata Certification   QlikView   News    Contact Section  You may contact us by email:  datawarehouse4u.info[at]gmail.com OLTP vs. OLAP We can divide IT systems into transactional (OLTP) and analytical (OLAP). In general we can assume that OLTP systems provide source data...
What's next for Microsoft's BizTalk integration server, services Another on-premises version of Microsoft's BizTalk enterprise integration server is coming next year. And Logic Apps will be hostable on-premises in 2019, too. BizTalk is Microsoft's application-integration server with an installed base of thousands of enterprise users. The server includes more than dozens of adapters for connecting systems inside and outside a customer's organization. The coming BizTalk Server "vNext" will be generally available "within roughly 9 months of the general availability of Windows Server 2019," the team announced on August 9 on its blog. Windows Server 2019 is expected to begin rolling out around October or so this year, from what we've heard, meaning the next BizTalk Server release should be in the latter half of 2019. BizTalk Server vNext will include all previously released feature packs; platform support for the newest version...