每天选择第一组10(删除重复记录)

时间:2012-09-27 19:39:06

标签: sql-server tsql

我有一个名为BillingInformation的表,其中包含以下列: id,clientId,type,order,value,timestamp

id是一个人工密钥 对于特定日期的每个clientId,将有10种类型,例如

clientId   type              order    timestamp
1          Outstanding AR    0        2012-09-24 10:45:28.557
1          Est. Days In AR   1        2012-09-24 10:45:28.580
1          ...               2        2012-09-24 10.45.28.603
1          ...               3        2012-09-24 10.45.28.620  
1          ...               4        2012-09-24 10.45.28.630  
1          ...               5        2012-09-24 10.45.28.653
1          ...               6        2012-09-24 10.45.28.697
1          ...               7        2012-09-24 10.45.28.717
1          ...               8        2012-09-24 10.45.28.727
1          ...               9        2012-09-24 10.45.28.727
2          Outstanding AR    0        ...
2          Est. Days In AR   1        ...
(cont. 8 more rows for client 2)

我的问题是我有一些clientIds,其中10个条目在数据库中不止一次,我只想为每个客户端选择10个(其中order = 0到9)。

现在是我的情况:

clientId   type              order    timestamp
1          Outstanding AR    0        2012-09-24 10:45:28.557
1          Est. Days In AR   1        2012-09-24 10:45:28.580
1          ...               2        2012-09-24 10.45.28.603
1          ...               3        2012-09-24 10.45.28.620  
1          ...               4        2012-09-24 10.45.28.630  
1          ...               5        2012-09-24 10.45.28.653
1          ...               6        2012-09-24 10.45.28.697
1          ...               7        2012-09-24 10.45.28.717
1          ...               8        2012-09-24 10.45.28.727
1          ...               9        2012-09-24 10.45.28.727
1          Outstanding AR    0        2012-09-24 10:45:28.557
1          Est. Days In AR   1        2012-09-24 10:45:28.580
1          ...               2        2012-09-24 10.45.28.603
1          ...               3        2012-09-24 10.45.28.620  
1          ...               4        2012-09-24 10.45.28.630  
1          ...               5        2012-09-24 10.45.28.653
1          ...               6        2012-09-24 10.45.28.697
1          ...               7        2012-09-24 10.45.28.717
1          ...               8        2012-09-24 10.45.28.727
1          ...               9        2012-09-24 10.45.28.727
2          Outstanding AR    0        ...
2          Est. Days In AR   1        ...

(续8个客户端2行) (续10个客户端3行) (依此类推......)

无论在2012-09-14那天为客户1重复记录0到9多少次(忽略小时分秒和毫秒),我只想选择一组10和另一组记录。因此,如果任何一个客户在同一个月和一年中都有10个重复的集合,那么我只想在那个月的一天和一年中找到一组10个。

任何人都可以帮我吗?

2 个答案:

答案 0 :(得分:1)

如下:

WITH CTE AS (
SELECT id, ROW_NUMBER() OVER (PARTITION BY ClientId, CAST([timestamp] AS date), [order] ORDER BY [timestamp]) as rn
FROM BillingInformation
)
SELECT b.*
FROM BillingInformation b
JOIN CTE c on c.id = b.id and c.rn=1

答案 1 :(得分:1)

select id, clientId, type, [order], value, timestamp
from
(
  select *, row_number() over (partition by clientId, [order],
                                            datediff(d,0,timestamp)
                               order by timestamp desc) rn
  from BillingInformation
) X
where rn=1