# XOÁ BỎ CÁC DÒNG TRÙNG

## Yêu cầu

* Xác định các dòng bị trùng nhau theo thông tin: Ví dụ, Trùng đối tượng công nợ theo cột mã số thuế
* Dồn dữ liệu phát sinh của các mã bị trùng về 1 mã
* Xoá các dòng danh mục đã dồn

## Hướng dẫn

* Tạo ra bảng tạm cte để xác định các dòng trùng theo yêu cầu và đánh số thứ tự
* Gộp dữ liệu các dòng stt> 1 về dòng có stt=1
* Xoá các dòng stt > 1

```sql
// Tạo bảng tạm cte gộp yếu tố trùng, đánh số stt 
WITH cte AS (
    SELECT ma,ten,vattu,identitykey, 
        ROW_NUMBER() OVER (PARTITION BY ma,ten ORDER BY identitykey) row_num
    FROM vattu
    Where vattu<>0 and ma<>''
)
// select bảng cte để kiểm tra
--select * from cte

// tạo câu lệnh select kiểm tra
--select T1.*, T2.vattu as vattuGoc from (select * from cte where row_num>1) T1 inner join (select * from cte where row_num=1)T2 on T1.ma=T2.ma and T1.ten=T2.ten

// tạo câu lệnh update dồn dữ liệu
update T1 set T1.vattu= T2.vattuGoc
from
(select * from dulieuketoan where vattu<>0) T1
inner join 
(
	select b1.*, b2.vattu as vattuGoc 
	from
	(select * from cte where row_num>1) b1 
	inner join 
	(select * from cte where row_num=1)b2 
	on b1.ma=b2.ma and b1.ten=b2.ten
)T2 
on T1.vattu = T2.vattu

// xoá các dòng có row_num> 1
delete cte where row_num>1
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kythuat.dtechvn.com/danh-muc/xoa-b-cac-dong-trung.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
