How to get top lines from a specific app. percent using SQL Server 2008?

I need approx. 30% of the data from each date.

id     name   datecol
-----------------------
1       A     2016-11-11
2       B     2016-11-11
3       C     2016-11-11
4       D     2016-11-11
5       E     2016-11-11
6       F     2016-11-11
7       G     2016-11-11
8       H     2016-11-11
9       I     2016-11-11
10      J     2016-11-11
11      A1    2016-11-12
12      B1    2016-11-12
13      C1    2016-11-12
14      D1    2016-11-13
15      E1    2016-11-13
16      F1    2016-11-14
17      G1    2016-11-14
18      H1    2016-11-14
19      I1    2016-11-14
20      J1    2016-11-14

In this case, I

10 lines in 2016-11-11
3 lines in 2016-11-12
2 lines in 2016-11-13
5 lines in 2016-11-14

I need this for approx. 30 percent of the top lines from each date,

id     name   datecol
-----------------------
1       A     2016-11-11
2       B     2016-11-11
3       C     2016-11-11
11      A1    2016-11-12
14      D1    2016-11-13
16      F1    2016-11-14
17      G1    2016-11-14

Thanks at Advance.

+4
source share
2 answers

Try this query using ROW_NUMBER () to get the row number and COUNT () OVER () to get the total for each date:

WITH CTE AS 
(
  SELECT T.*,
         ROW_NUMBER() OVER (PARTITION BY datecol ORDER BY Name) as RowNum,
         COUNT(*) OVER (PARTITION BY datecol) as Total
    FROM Table as T
)
SELECT id,name,datecol 
  FROM CTE 
 WHERE RowNum <= CEILING(Total*0.30)

Result:

1   A   2016-11-11
2   B   2016-11-11
3   C   2016-11-11
11  A1  2016-11-12
14  D1  2016-11-13
16  F1  2016-11-14
17  G1  2016-11-14
+5
source
;with cte as (
    Select *
          ,RN=Row_Number() over (Partition By datecol Order By datecol)
    From   YourTable
)
Select A.* 
 From cte A
 Join (Select datecol,cnt=count(*) from YourTable Group By datecol) B
   on A.datecol=B.datecol 
   and A.RN<=ceiling(B.cnt*.3)
 Order by datecol,RN

Returns

id  name    datecol     RN
1   A       2016-11-11  1
2   B       2016-11-11  2
3   C       2016-11-11  3
11  A1      2016-11-12  1
14  D1      2016-11-13  1
16  F1      2016-11-14  1
17  G1      2016-11-14  2
+2

Source: https://habr.com/ru/post/1662408/


All Articles