将两个类似的查询合并为一个查询

时间:2012-11-09 03:48:16

标签: database

我需要将这两个类似的查询合并到一个查询中

1

SELECT avg(size) 
FROM (
    SELECT  size,addr,sha1 
    FROM data WHERE id >= (abs(random()) % (SELECT max(id) FROM data)) LIMIT 8000
);

2

SELECT sum(size2 * cnt), sum(cnt)  
FROM (
    SELECT size as size2, sha1, COUNT (*) as cnt 
    FROM 
        (
        SELECT size,addr,sha1 
        FROM data 
        WHERE 
            id >= (abs(random()) % (SELECT max(id) FROM data)) LIMIT 8000
        ) 
    GROUP BY sha1, size 
    HAVING count(*)>=2
    );

我会尝试这样,但可能不正确。

SELECT sum(size2 * cnt), sum(cnt),  avg(sizeavg) 
FROM 
    (
    SELECT sizeavg, size as size2, sha1, COUNT (*) as cnt 
    FROM 
        (
        SELECT size, size as sizeavg,addr,sha1 
        FROM data 
        WHERE id >= (abs(random()) % (SELECT max(id) FROM data)) 
        LIMIT 8000
        ) 
    GROUP BY sha1, size 
    HAVING count(*)>=2
);

0 个答案:

没有答案