[英]How to count the number of duplicate in sql
我有如下表我想计算重复并分配它们。
customer date type
A 2020/8/1 a
A 2020/8/1 b
B 2019/5/1 a
B 2019/6/1 c
我想count
customer
和date
中的重复,分配它们的duplicate
数量。
["customer,"date"]
customer date type duplicate
A 2020/8/1 a 2
A 2020/8/1 b 2
B 2019/5/1 a 1
B 2019/6/1 c 1
如果有人有意见分配重复的行数。 请告诉我。 谢谢
使用count() over
分析 function。 Date
是保留字
SELECT c.*,
COUNT(*) OVER (PARTITION BY "customer","date") duplicate
FROM tablename c;
如果你想格式化日期列
SELECT "customer",
to_char("date",'DD-MON-YYYY HH:MI:SS AM') "date",
"type",
COUNT(*) OVER (PARTITION BY "customer","date") duplicate
FROM tablename ;
你可以试试下面的 -
select a.customer,a.date,type,duplicate
from tablename a
join
(select customer,date, count(*) as duplicate from tablename group by customer, date) b
on a.customer=b.customer and a.date=b.date
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.