我有下面的示例表,其中我在不同时间戳收到了重复的 ID。 我需要计算一天内收到的不同 ID 的数量以及当天收到的重复项的数量
select trunc(date) ,count(distinct id) ,COUNT(*) AS COUNTS
from abc
where trunc(date)=trunc(sysdate)
group by trunc(date)
HAVING COUNT(*)>1;
下表是示例数据。
date id
1/1/2024 1011
1/1/2024 1012
1/1/2024 1011
1/1/2024 1014
1/2/2024 1014
1/2/2024 1014
我需要得到以下结果。我无法获得当天的重复总数。
date distinct id no. of duplicates on that day
1/1/2024 3 2
1/2/2024 1 2
我在 CTE 中完成了这两项计算,然后在日期上为您将它们合并在一起。
WITH CTE
AS (
SELECT
[date],
COUNT(DISTINCT ID) as 'distinct id'
FROM
MyTable
group by
[date]
),
CTE2
AS (
SELECT
date,
id,
COUNT(id) as 'no. of duplicates on that day'
FROM
MyTable T
group by
date,
id
HAVING
COUNT(id) > 1
)
SELECT
CTE2.date,
[distinct id],
[no. of duplicates on that day]
FROM
CTE2
JOIN
CTE
ON CTE.date = CTE2.date