0

特定の基準に対して複数の ID を持つ SQL テーブルがあります。

mysql> select distinct id from FILTER where ft='f' and timestamp between '1367539200000' and '1367625599999';
+-----+
| id  |
+-----+
| 0   |
| 121 |
| 122 |
| 124 |
| 125 |
| 127 |
+-----+
6 rows in set (0.00 sec)

mysql>

6行すべての結果を返すクエリを実行したい:

mysql> select a.id, count(a.id) as total, SUM(CASE WHEN b.result='0' THEN 1 ELSE 0 END) AS allowed, SUM(CASE WHEN b.result='1' THEN 1 ELSE 0 END) AS modified, SUM(CASE WHEN b.result='2' THEN 1 ELSE 0 END) AS blocked from FILTER a INNER JOIN EVENTS b on a.fid = b.event_id and a.timestamp = b.timestamp where b.protocol_type='MM4' and a.ft='f' and a.id in ('0', '121', '122', '124', '125', '127') and a.fid=b.event_id and b.timestamp between '1367539200000' and '1367625599999';
+----+-------+---------+----------+---------+
| id | total | allowed | modified | blocked |
+----+-------+---------+----------+---------+
| 0  |   216 |       0 |      135 |      81 |
+----+-------+---------+----------+---------+
1 row in set (0.01 sec)

mysql>

これは最初の行のみを返します。他の ID の 1 つがフィルター基準を満たしていることを確認できます (実際、すべての ID が満たしています)。

mysql> select a.id, count(a.id) as total, SUM(CASE WHEN b.result='0' THEN 1 ELSE 0 END) AS allowed, SUM(CASE WHEN b.result='1' THEN 1 ELSE 0 END) AS modified, SUM(CASE WHEN b.result='2' THEN 1 ELSE 0 END) AS blocked from FILTER a INNER JOIN EVENTS b on a.fid = b.event_id and a.timestamp = b.timestamp where b.protocol_type='MM4' and a.ft='f' and a.id='127' and a.fid=b.event_id and b.timestamp between '1367539200000' and '1367625599999';
+-----+-------+---------+----------+---------+
| id  | total | allowed | modified | blocked |
+-----+-------+---------+----------+---------+
| 127 |    24 |       0 |       15 |       9 |
+-----+-------+---------+----------+---------+
1 row in set (0.00 sec)

mysql>

私もインリストなしで試しました:

mysql> select a.id, count(a.id) as total, SUM(CASE WHEN b.result='0' THEN 1 ELSE 0 END) AS allowed,  SUM(CASE WHEN b.result='1' THEN 1 ELSE 0 END) AS modified, SUM(CASE WHEN b.result='2' THEN 1 ELSE 0 END) AS blocked from FILTER a INNER JOIN EVENTS b on a.fid = b.event_id and a.timestamp = b.timestamp where b.protocol_type='MM4' and a.ft='f' and a.fid=b.event_id and b.timestamp between '1367539200000' and '1367625599999';
+----+-------+---------+----------+---------+
| id | total | allowed | modified | blocked |
+----+-------+---------+----------+---------+
| 0  |   216 |       0 |      135 |      81 |
+----+-------+---------+----------+---------+
1 row in set (0.00 sec)

mysql>

最初の行だけでなく、ID ごとに 6 行を表示するようにフィルターを変更するにはどうすればよいですか?

4

4 に答える 4

0

このようなもの:

select 
    a.id, 
    count(a.id) as total, 
    SUM(
        CASE WHEN b.result='0' THEN 1 
        ELSE 0 
        END
    ) AS allowed, 
    SUM(
        CASE WHEN b.result='1' THEN 1 
        ELSE 0 
        END
    ) AS modified, 
    SUM(
        CASE WHEN b.result='2' THEN 1 
        ELSE 0 
        END
    ) AS blocked 
from FILTER a 
    INNER JOIN EVENTS b on a.fid = b.event_id 
        and a.timestamp = b.timestamp 
where b.protocol_type='MM4' 
    and a.ft='f' 
    and a.id='127' 
    and a.fid=b.event_id 
    and b.timestamp between '1367539200000' and '1367625599999'
group by a.id;
于 2013-05-03T14:23:50.270 に答える