如何按列只获得每组10%的记录?

时间:2017-09-26 11:23:47

标签: sql sql-server-2012

让我解释整个问题。 假设我有一个如下所示的表

    Id                   Box    Folder   Status 
    -------------------- ------ -------- ----------
    493                  77777  9995     1          
    494                  77777  9996     1          
    494                  77777  9996     1          
    494                  77777  9996     1          
    494                  77777  9996     1          
    494                  77777  9996     1 
    494                  77777  9996     1 
    494                  77777  9996     1     
    494                  77777  9996     1     
    494                  77777  9996     1     
    497                  88888  9999     1          
    498                  88888  0000     0       
    498                  88888  0000     0       
    498                  88888  0000     0       
    498                  88888  0000     0       
    495                  99999  9997     1          
    496                  99999  9998     1          
    496                  99999  9998     1          
    497                  88888  9999     1          
    498                  88888  0000018  0   
    498                  88888  0000018  0   
    498                  88888  0000018  0          
    499                  66666  0000020  1          
    502                  66666  1000     1          

现在我需要结果,比如获得每个“boxNo”的10%记录

例如boxno“77777”有10条记录。 10记录的10%是1记录。所以只能看到1条记录。

在我看来只有用光标和临时表来做这件事。 任何人都可以建议以更简单的方式获得这些结果吗?

2 个答案:

答案 0 :(得分:3)

您可以使用窗口功能执行此操作。虽然有$('#imageId').load( function () { }); 和`percentile()函数,但我会明确地计算:

ntile()

您可能希望确保至少获得一条记录。如果是这样的话:

select t.*
from (select t.*,
             row_number() over (partition by box order by newid()) as seqnum,
             count(*) over (partition by box) as cnt
      from t
     ) t
where seqnum <= 0.1 * cnt;

答案 1 :(得分:2)

您可以使用TOP PERCENT

获取数据
DECLARE @Table TABLE (Id INT, Box VARCHAR(10), Folder VARCHAR(10) , Status INT)
INSERT INTO @Table 
VALUES
(493,'77777','9995',1),
(494,'77777','9996',1),
(494,'77777','9996',1),
(494,'77777','9996',1),
(494,'77777','9996',1),
(494,'77777','9996',1), 
(494,'77777','9996',1), 
(494,'77777','9996',1),
(494,'77777','9996',1),
(494,'77777','9996',1),
(497,'88888','9999',1),
(498,'88888','0000',0),
(498,'88888','0000',0),
(498,'88888','0000',0),
(498,'88888','0000',0),
(495,'99999','9997',1),
(496,'99999','9998',1),
(496,'99999','9998',1),
(497,'88888','9999',1),
(498,'88888','0000018',0),
(498,'88888','0000018',0),
(498,'88888','0000018',0),
(499,'66666','0000020',1),
(502,'66666','1000',1)

SELECT X.* FROM (SELECT DISTINCT Box FROM @Table) T
CROSS APPLY (SELECT TOP 10 PERCENT * FROM @Table WHERE Box = T.Box ORDER BY ID) X

结果:

Id          Box        Folder     Status
----------- ---------- ---------- -----------
499         66666      0000020    1
493         77777      9995       1
497         88888      9999       1
495         99999      9997       1