0

このクエリから上位 5 つの「ID」を取得するために必要なもの。何が起こっているのかというと、トップ 5 ではなく 1 つの ID を取得しています。これを修正する方法を知っている人はいますか?クエリは次のとおりです。

(
SELECT id,
     firstName,
     lastName,
     SUM(fileSize) AS TotalBytes,
     SUM(fileSize)/COUNT(*) AS Average
FROM   roster_cs368 AS a
JOIN htmp_cs368  AS b USING (id)
) UNION (
SELECT id,
     firstName,
     lastName,
     SUM(fileSize) AS TotalBytes,
     SUM(fileSize)/COUNT(*) AS Average
FROM   roster_cs368 AS a
JOIN atmp_cs368  AS b USING (id)
)
ORDER BY TotalBytes DESC
LIMIT 5

ありがとう!これも大きなプログラムの一部であるため、java タグが付けられています。

スキーマは次のとおりです。

mysql> select * from roster_cs368
-> ;
+--------+-----------+-----------+
| id     | firstName | lastName  |
+--------+-----------+-----------+
| apn7cf | Allen     | Newton    |
| atggg3 | andrew    | goebel    |

主キーはどこですか、

mysql> select * from htmp_cs368;
+------------+----------+------------+----------+----------+-------+------+-------+----------------------+
| filePerms  | numLinks | id         | idGroup  | fileSize | month | day  | time  | fileName             |
+------------+----------+------------+----------+----------+-------+------+-------+----------------------+
| drwx------ |        2 | schulte    | faculty  |      289 | Nov   |    7 | 2011  | Java                 |
| -rw-r--r-- |        1 | schulte    | faculty  |      136 | Apr   |   29 | 2012  | LD                   |
| drwxr-xr-x |        3 | schulte    | faculty  |      177 | Mar   |   20 | 2012  | Upgrade              |

ここには主キーはありません。

mysql> select * from atmp_cs368;
+------------+----------+--------------+----------+----------+-------+------+-------+-----------------------------+
| filePerms  | numLinks | id           | idGroup  | fileSize | month | day  | time  | fileName                    |
+------------+----------+--------------+----------+----------+-------+------+-------+-----------------------------+
| drwxr-xr-x |        2 | remierm      | 203      |      245 | Sep   |   17 | 14:40 | 148360_sun_studio_12        |
| drwx---rwx |       31 | antognolij   | sasl     |     2315 | Oct   |   24 | 12:28 | 275                         |
| -rwx------ |        1 | kyzvdb       | student  |       36 | Sep   |   19 | 13:05 | 275hh                       |

ここには主キーはありません。

私のクエリは何ですか:

produce a list of the five members of roster_cs368,
    and their ids who use the most space (number of bytes)
    in htmp_cs368 and atmp_cs368 in descending order--
    greediest first.  display total number of bytes and
    average size of file
4

1 に答える 1

0

あなたはこのようにそれを行うことができます:

SELECT * FROM(
(
SELECT id,
     firstName,
     lastName,
     SUM(fileSize) AS TotalBytes,
     SUM(fileSize)/COUNT(*) AS Average
FROM   roster_cs368 AS a
JOIN htmp_cs368  AS b USING (id)
) UNION (
SELECT id,
     firstName,
     lastName,
     SUM(fileSize) AS TotalBytes,
     SUM(fileSize)/COUNT(*) AS Average
FROM   roster_cs368 AS a
JOIN atmp_cs368  AS b USING (id)
)
) as somealias ORDER BY TotalBytes DESC
LIMIT 5
于 2012-12-02T22:10:18.703 に答える