類似したクエリがいくつかあり、条件句のみが異なります。すべてのクエリを 1 つのクエリに結合しようとしています。
最初のクエリは次のようになります。
最初のクエリ:
SELECT pages.title,
YEAR(page_revisions.date) AS year,
MONTH(page_revisions.date) AS MONTH,
COUNT(revid) AS Cadmin_edits
FROM page_revisions
INNER JOIN pages ON page_revisions.title=pages.title
WHERE userid!=0 AND admin=1 AND pages.ns=0
GROUP BY year, MONTH
ORDER BY page_revisions.title, year, MONTH;
+--------------+------+-------+--------------+
| title | year | MONTH | Cadmin_edits |
+--------------+------+-------+--------------+
| Bangalore | 2002 | 11 | 4 |
| Bangalore | 2003 | 2 | 2 |
| Bangalore | 2003 | 5 | 1 |
| Bangalore | 2003 | 6 | 6 |
| Bangalore | 2003 | 8 | 4 |
| Bangalore | 2003 | 9 | 3 |
| Bangalore | 2003 | 10 | 2 |
| Bart Simpson | 2002 | 9 | 4 |
| Bart Simpson | 2007 | 3 | 33 |
| Bart Simpson | 2007 | 7 | 15 |
| Bart Simpson | 2008 | 11 | 19 |
| Batman | 2001 | 12 | 5 |
2 番目のクエリは非常に似ていますが、admin ではなく wikip_member=1 という条件のみが異なります。
2 番目のクエリ:
SELECT pages.title,
YEAR(page_revisions.date) AS year,
MONTH(page_revisions.date) AS MONTH,
COUNT(revid) AS Wpart_edits
FROM page_revisions
INNER JOIN pages ON page_revisions.title=pages.title
WHERE userid!=0 AND wikip_member=1 AND pages.ns=0
GROUP BY year, MONTH
ORDER BY page_revisions.title, year, MONTH;
+------------------------------------------+------+-------+-------------+
| title | year | MONTH | Wpart_edits |
+------------------------------------------+------+-------+-------------+
| 1906 Florida Keys hurricane | 2011 | 10 | 266 |
| 1906 Florida Keys hurricane | 2011 | 11 | 108 |
| 1906 Florida Keys hurricane | 2012 | 2 | 66 |
| 1969 race riots of Singapore | 2007 | 6 | 124 |
| 1969 race riots of Singapore | 2008 | 2 | 143 |
| 1969 race riots of Singapore | 2009 | 1 | 151 |
| 1980 Winter Olympics | 2003 | 8 | 3 |
| 1980 Winter Olympics | 2003 | 10 | 7 |
| 1980 Winter Olympics | 2004 | 3 | 16 |
| Babe Ruth | 2004 | 5 | 22 |
| Babe Ruth | 2004 | 7 | 21 |
| Bangalore | 2002 | 11 | 6 |
| Bangalore | 2003 | 2 | 2 |
| Bangalore | 2004 | 2 | 9 |
基本的に、出力を次のようにする必要があります。
意図した出力:
+------------------------------------------+------+-------+--------+-------+
| title | year | MONTH | Cadmin | Wpart |
+------------------------------------------+------+-------+--------+-------+
| 1906 Florida Keys hurricane | 2011 | 10 | 0 | 266 |
| 1906 Florida Keys hurricane | 2011 | 11 | 0 | 108 |
| 1906 Florida Keys hurricane | 2012 | 2 | 0 | 66 |
| 1969 race riots of Singapore | 2007 | 6 | 0 | 124 |
| 1969 race riots of Singapore | 2008 | 2 | 0 | 143 |
| 1969 race riots of Singapore | 2009 | 1 | 0 | 151 |
| 1980 Winter Olympics | 2003 | 8 | 0 | 3 |
| 1980 Winter Olympics | 2003 | 10 | 0 | 7 |
| 1980 Winter Olympics | 2004 | 3 | 0 | 16 |
| Babe Ruth | 2004 | 4 | 0 | 7 |
| Babe Ruth | 2004 | 8 | 0 | 21 |
| Bangalore | 2002 | 11 | 4 | 6 |
| Bangalore | 2003 | 2 | 2 | 2 |
| Bangalore | 2003 | 5 | 1 | 0 |
| Bangalore | 2003 | 6 | 6 | 0 |
| Bangalore | 2003 | 8 | 4 | 0 |
| Bangalore | 2003 | 9 | 3 | 0 |
| Bangalore | 2003 | 10 | 2 | 0 |
| Bangalore | 2004 | 2 | 0 | 9 |
これは、クエリの組み合わせになります。基本的には、各月のリビジョンの COUNT を取得し、その月に条件に一致するリビジョンが見つからない場合は 0 を表示します。UNION、SUBSELECT などのいくつかの方法を試しましたが、成功しませんでした。UNION は意図したとおりに機能していないようで、SUBSELECT は 128 行だけを実行するのに 5 時間かかりました。
ユニオンの試み:
SELECT pages.title,
YEAR(page_revisions.date) AS year,
MONTH(page_revisions.date) AS MONTH,
COUNT(revid) AS Cadmin,
null AS Wpart
FROM page_revisions
INNER JOIN pages ON page_revisions.title=pages.title
WHERE userid!=0 AND admin=1 AND pages.ns=0
UNION
SELECT pages.title,
mid( page_revisions.date, 1, 4) AS year,
mid(page_revisions.date, 6, 2) AS MONTH,
null as Cadmin,
COUNT(revid) AS Wpart_edits
FROM page_revisions
INNER JOIN pages ON page_revisions.title=pages.title
WHERE userid!=0 AND wikip_member=1 AND pages.ns=0
GROUP BY year, MONTH
ORDER BY title, year, MONTH;
サブセレクト試行:
SELECT pages.title,
YEAR(page_revisions.date) AS year,
MONTH(page_revisions.date) AS MONTH,
(SELECT COUNT(revid) AS Cadmin_edits FROM page_revisions WHERE YEAR(page_revisions.date)=year and MONTH(page_revisions.date)= month and userid!=0 AND admin=1)
FROM page_revisions
INNER JOIN pages ON page_revisions.title=pages.title
WHERE pages.ns=0
GROUP BY year, MONTH
ORDER BY pages.title, year, MONTH;
私を正しい方向に向ける助けがあれば、大歓迎です。これは、金曜日に予定されている最終プレゼンテーション用であり、すべてをまとめるための最後のクエリです。