1
給出的以下結構的表:SQL服務器查詢通過
subscriber_id,帶
11,1
12,1
13,1
...
21,2
22.2
23.2
24.2
...
N1,正
N2,正
N3,正
...
nm,n
我想從每個組中獲得n%大小的用戶子組。對於10%,我應該得到組1的10%,組2的10%...組n的10%。
給出的以下結構的表:SQL服務器查詢通過
subscriber_id,帶
11,1
12,1
13,1
...
21,2
22.2
23.2
24.2
...
N1,正
N2,正
N3,正
...
nm,n
我想從每個組中獲得n%大小的用戶子組。對於10%,我應該得到組1的10%,組2的10%...組n的10%。
這聽起來像你想要一個分層樣本。您可以通過在每個組中首先枚舉並隨後選擇所需的「n」個記錄來獲得此信息。這裏是如何做到這一點在SQL Server中的例子:
select t.id, t.band
from (select t.*,
row_number() over (order by band_seqnum) as seqnum
from (select t.*,
row_number() over (partition by band order by rand(checksum()) as band_seqnum,
count(*) over() as cnt
from t
) t
) t
where band_seqnum <= 0.10 * cnt;
嘗試這個
Select * from
(
Select *, NTILE(n%) over(partition by id order by id) 'R' from t)t
where t.R<=(n%)
你們是不是要執行的一個語句或批處理SQL的?此外,[這](http://msdn.microsoft.com/en-us/library/ms189461.aspx)有幫助嗎? – Bmo
你可以檢查下面給出的代碼,如果沒有問題還可以恢復 – Azar