我有一个名为Sales
的表:
+----------+-----------------+------------+
| Salesman | Sales Portfolio | Month |
+----------+-----------------+------------+
| Kavi | 12500 | 2018-01-05 |
| Kavi | 12500 | 2018-02-28 |
| Kavi | 12500 | 2018-03-20 |
| Raj | 21055 | 2018-01-05 |
| Raj | 32015 | 2018-02-28 |
| Raj | 12000 | 2018-03-20 |
+----------+-----------------+------------+
如果Sales Portfolio
值重复,请从表中删除包括其自身在内的所有行。在上面的示例中,12500
是重复的,因此请删除Sales Portfolio = 12500
。
答案 0 :(得分:3)
如果您只想显示预期输出,请尝试以下操作:
WITH cte AS (
SELECT *,
COUNT(*) OVER (PARTITION BY Salesman, [Sales Portfolio]) cnt
FROM yourTable
)
SELECT
Salesman, [Sales Portfolio], Month
FROM cte
WHERE cnt = 1;
如果你想删除非显示记录,那么我们可以使用相同的CTE:
DELETE FROM cte WHERE cnt > 1;