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)

私の問題は、データベースに 10 個のエントリが複数回存在する clientIds があり、クライアントごとに 10 個 (順序 = 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 行) (など...)

2012 年 9 月 14 日のクライアント 1 のレコード 0 から 9 が何回繰り返されても (時間、分、秒、ミリ秒は無視して)、他のレコードと一緒に 10 のグループを 1 つだけ選択したいだけです。したがって、クライアントが同じ月日と年に 10 個のセットを複製している場合、その月日と年に 10 個のグループが 1 つだけ必要です。

誰でもこれで私を助けることができますか?

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 に答える