SQL选择随机插入的分组结果

时间:2017-04-04 13:00:58

标签: sql sqlite random group-by sql-order-by

对于具有ID和类别列的表,我们将有一个具有5个值的类别域,例如A,B,C,D和E.

我们正在使用SQLite。

我们的目标是让查询带来随机选择并随机排序的多个插入的分组行(在每组5行内),例如

ID    CAT
---   ---
076    C
753    D
503    A 
081    B 
335    E

475    B
643    D
995    A
105    E
549    C

553    E
871    B
064    C
720    D
119    A

上面的例子显示了3组os结果,其行是随机选择的,其排序也是随机的。

目前,我们使用5个查询(每个类别一个)查询结果,然后对应用程序代码进行排序。

SELECT * FROM table WHERE cat ='A'ORDED BY RANDOM()LIMIT 3;

如果有人通过使用单个查询建议更好的解决方案,我们将不胜感激。

3 个答案:

答案 0 :(得分:0)

您可以对3个联盟使用以下查询,它不是很短但是它可以完成它的工作:

SELECT randomized.id, randomized.cat
FROM (
    SELECT randomized.id, randomized.cat
    FROM (
        SELECT id,cat FROM `table_test` ORDER BY RANDOM() 
        ) AS randomized
    GROUP BY cat
    ORDER BY RANDOM()
    ) AS randomized
UNION ALL
SELECT randomized.id, randomized.cat
FROM (
    SELECT randomized.id, randomized.cat
    FROM (
        SELECT id,cat FROM `table_test` ORDER BY RANDOM() 
        ) AS randomized
    GROUP BY cat
    ORDER BY RANDOM()
    ) AS randomized
UNION ALL
SELECT randomized.id, randomized.cat
FROM (
    SELECT randomized.id, randomized.cat
    FROM (
        SELECT id,cat FROM `table_test` ORDER BY RANDOM() 
        ) AS randomized
    GROUP BY cat
    ORDER BY RANDOM()
    ) AS randomized    

答案 1 :(得分:0)

您可以使用五个临时表将autoincrementing ID分配给每个类别中的行:

CREATE TEMPORARY TABLE a(nr INTEGER PRIMARY KEY, id);
CREATE TEMPORARY TABLE b(nr INTEGER PRIMARY KEY, id);
...
INSERT INTO temp.a(id) SELECT id FROM MyTable WHERE cat = 'A' ORDER BY random();
INSERT INTO temp.b(id) SELECT id FROM MyTable WHERE cat = 'B' ORDER BY random();
...

然后,您可以组合五个表,以便将具有相同nr值的行排序在一起:

SELECT nr, id, 'A' AS cat, random() AS r FROM temp.a
UNION ALL
SELECT nr, id, 'B',        random()      FROM temp.b
UNION ALL
...
ORDER BY nr, r;

答案 2 :(得分:0)

开发了一种获得我们需要的结果的方法:连续的行块,每个块都没有' cat'重复,没有' id'完全重复。

它比面向查询更具数学导向性,但无论如何它都符合我们的需求。

CREATE TEMPORARY TABLE a(nr INTEGER PRIMARY KEY, sort, id, cat);

INSERT INTO temp.a(id, cat) SELECT id, cat FROM tabela WHERE cat = 'A' ORDER BY random() limit 3;
INSERT INTO temp.a(id, cat) SELECT id, cat FROM tabela WHERE cat = 'B' ORDER BY random() limit 3;
INSERT INTO temp.a(id, cat) SELECT id, cat FROM tabela WHERE cat = 'C' ORDER BY random() limit 3;
INSERT INTO temp.a(id, cat) SELECT id, cat FROM tabela WHERE cat = 'D' ORDER BY random() limit 3;
INSERT INTO temp.a(id, cat) SELECT id, cat FROM tabela WHERE cat = 'E' ORDER BY random() limit 3;

UPDATE temp.a SET sort=100 where nr=1;
UPDATE temp.a SET sort=92+(select abs(random()%(2))*16) where nr=4;
UPDATE temp.a SET sort=88+(select abs(random()%(4))*8) where nr=7;
UPDATE temp.a SET sort=86+(select abs(random()%(8))*4) where nr=10;
UPDATE temp.a SET sort=85+(select abs(random()%(16))*2) where nr=13;

UPDATE temp.a SET sort=200 where nr=2;
UPDATE temp.a SET sort=192+(select abs(random()%(2))*16) where nr=5;
UPDATE temp.a SET sort=188+(select abs(random()%(4))*8) where nr=8;
UPDATE temp.a SET sort=186+(select abs(random()%(8))*4) where nr=11;
UPDATE temp.a SET sort=185+(select abs(random()%(16))*2) where nr=14;

UPDATE temp.a SET sort=300 where nr=3;
UPDATE temp.a SET sort=292+(select abs(random()%(2))*16) where nr=6;
UPDATE temp.a SET sort=288+(select abs(random()%(4))*8) where nr=9;
UPDATE temp.a SET sort=286+(select abs(random()%(8))*4) where nr=12;
UPDATE temp.a SET sort=285+(select abs(random()%(16))*2) where nr=15;

SELECT id, cat FROM temp.a ORDER BY sort;

DROP TABLE a;