0

我有一个名为 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        ...

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

无论客户 1 在 2012 年 9 月 14 日这一天重复多少次记录 0 到 9(不考虑时分秒和毫秒),我只想选择一组 10 条记录和其他记录。因此,如果任何客户在同一月份和同一年重复了 10 组,我只想要在该月份和同一年的一组 10 组。

谁能帮我这个?

4

2 回答 2

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
于 2012-09-27T20:04:12.137 回答
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
于 2012-09-27T20:14:46.907 回答