如何在单打列中获取不同值的有限数据集?

时间:2018-10-25 12:56:51

标签: mysql sql

假设我有表T,它有3列,而第3列包含10个不同的值,我想从表T中为第3列中的每个不同的值提取5个样本。


enter image description here

1 个答案:

答案 0 :(得分:0)

WITH T(A, B,C) AS(
SELECT 12,'Hi','One' FROM DUAL UNION ALL
SELECT 34,'Am','One' FROM DUAL UNION ALL
SELECT  6,'to','Two' FROM DUAL UNION ALL
SELECT  3,'do','Two' FROM DUAL UNION ALL
SELECT  5,'aim','Two' FROM DUAL UNION ALL
SELECT  6,'mine','Two' FROM DUAL UNION ALL
SELECT  4,'not','Three' FROM DUAL 
)
select a,b,c from(
SELECT A,B,C,ROW_NUMBER() OVER(PARTITION BY c ORDER BY C DESC) AS R
FROM T)
where r = 1

或者您可以尝试以下方法:

 SELECT MIN(T.A),  
         T.B, 
         t.c
    FROM t
    JOIN (SELECT C,
                 MAX(b) AS b
            FROM T
        GROUP BY C) Y ON Y.C = T.C
                     AND Y.B = T.B
GROUP BY t.c, t.b