I'll try to simplify this for simplicity's sake. I need to get the average value of a returned column of a query. Make sense? I'll try to elaborate.(Sample results borrowed from another question)
Plant_ID | Year |Quarter| MR | Range
| CCAR | 2009 | 1 | 706 | Null
| CCAR | 2009 | 2 | 626 | 0,08
| CCAR | 2009 | 2 | 637 | 0,11
| CCAR | 2009 | 2 | 737 | 0,1
| CCAR | 2009 | 1 | 552 | 0,19
| CCAR | 2009 | 4 | 418 | 0,137
| CCAR | 2009 | 1 | 503 | 0,085
| CCAR | 2009 | 2 | 645 | 0,058
| CCAR | 2009 | 4 | 743 | 0,098
| CCAR | 2009 | 3 | 556 | 0,187
| CCAR | 2009 | 1 | 298 | 0,258
| CCAR | 2009 | 2 | 339 | 0,041
| CCAR | 2010 | 1 | 381 | 0,042
I would get this result when I run a query like this
Select PlantID, Year, Quarter, MR, Range FROM TestTable WHERE PlantID in('CCAR')
I want the average MR for each quarter. Preliminarily I would try something like this.
Select Quarter, AVG(MR) FROM (Select PlantID, Year, Quarter, MR, Range FROM TestTable WHERE PlantID in ('CCAR')) GROUP BY Quarter ORDER BY Quarter
The issue is that I don't know where to nest the query to accomplish this. Any help?
Thanks!
Add an alias e.g. hallo
Select Quarter, AVG(MR) FROM
(Select PlantID, Year, Quarter, MR, Range FROM TestTable WHERE PlantID in ('CCAR')) hallo
GROUP BY Quarter ORDER BY Quarter
while this is not necessary in this case, the following would be enough
Select Quarter, AVG(MR)
FROM TestTable
WHERE PlantID in ('CCAR')
GROUP BY Quarter
ORDER BY Quarter
Just use aggregation:
select Year, Quarter, avg(MR)
from TestTable
where PlantID in ('CCAR')
group by Year, Quarter;
I presume you want the year with the quarter. If not, just remove year from the select and group by.
Related
Sample table tbl_name:
| ID | Name | Month | Quarter | Year |
| 1 | A | Jan | 1 | 2019 |
| 1 | A | Feb | 1 | 2019 |
| 2 | B | May | 2 | 2019 |
| 3 | C | May | 2 | 2018 |
Hi, this is the table I extract using SELECT query. I can find the distinct name per year using SELECT distinct name, year FROM tbl_name; But I'm trying to add a column during SELECT query to identify or count the unique occurrence per year of the name.
Expected:
| ID | Name | Month | Quarter | Year | Unique Count |
| 1 | A | Jan | 1 | 2019 | 1 |
| 1 | A | Feb | 1 | 2019 | 0 |
| 2 | B | May | 2 | 2019 | 1 |
| 3 | C | May | 2 | 2018 | 1 |
I tried splitting into two queries - one select everything; the other select just distinct and join them together but that will introduce duplicates. Is there a way to do this using SQL?
If you are running MySQL 8.0, you can use row_number() to flag the appearance of a name in a year:
select
t.*,
(row_number() over(
partition by name, year
order by str_to_date(concat(year, '-', month), '%Y-%b')
) = 1) unique_count
from mytable t
Note: do consider fixing the storage strategy of your date columns. Rather than splitting the information over several columns, you would better have a unique column in the relevant DATE datatype to store that information. That would save you the pain of recomposing the date when you need it.
Demo on DB Fiddle:
ID | Name | Month | Quarter | Year | unique_count
-: | :--- | :---- | ------: | ---: | -----------:
1 | A | Feb | 1 | 2019 | 1
1 | A | Jan | 1 | 2019 | 0
2 | B | May | 2 | 2019 | 1
3 | C | May | 2 | 2018 | 1
You can try this below logic-
DEMO HERE
WITH your_table(ID,Name,Month,Quarter,Year)
AS
(
SELECT 1,'A','Jan',1,2019 UNION ALL
SELECT 1,'A','Feb',1,2019 UNION ALL
SELECT 2,'B','May',2,2019 UNION ALL
SELECT 3,'C','May',2,2018
)
,CTE AS
(
SELECT *,ROW_NUMBER() OVER(PARTITION BY ID ORDER BY Quarter,Year) RN
FROM your_table
)
SELECT ID,Name,Month,Quarter,Year,
CASE WHEN RN = 1 THEN 1 ELSE 0 END Unique_Count
FROM CTE
Output is-
ID Name Month Quarter Year Unique_Count
1 A Jan 1 2019 1
1 A Feb 1 2019 0
2 B May 2 2019 1
3 C May 2 2018 1
t_table looks like:
+-----------+---------+--------------+------------------+-----------------------+----------------------------------+
| pk_IdLoan | fk_IdCar| fk_IdCustomer| fk_Source_Agency | fk_Destination_Agency | RentalDate | DeliveryDate | Cost |
+-----------+---------+--------------+------------------+-----------------------+----------------------------------+
I wrote a query:
(SELECT fk_IdCustomer, MONTHNAME(RentalDate) AS Month, YEAR(RentalDate) As Year, COUNT(*)
FROM t_loan
GROUP BY fk_IdCustomer, Month, Year);
which results in
+---------------+-------------+------+----------+
| fk_IdCustomer | Month | Year | COUNT(*) |
+---------------+-------------+------+----------+
| 1 | July | 2016 | 3 |
| 1 | November | 2017 | 1 |
| 1 | September | 2016 | 7 |
| 5 | May | 2016 | 1 |
| 6 | January | 2016 | 1 |
| 6 | September | 2017 | 2 |
+---------------+-------------+------+----------+
Now I want to get these months and years for each customer which result in highest COUNT(*), f.e.:
+---------------+-------------+------+----------+
| fk_IdCustomer | Month | Year | COUNT(*) |
+---------------+-------------+------+----------+
| 1 | September | 2016 | 7 |
| 5 | May | 2016 | 1 |
| 6 | September | 2017 | 2 |
+---------------+-------------+------+----------+
How to achieve this?
This is a bit painful in MySQL, which doesn't support CTEs or window functions. One method is:
SELECT fk_IdCustomer, MONTHNAME(RentalDate) AS Month,
YEAR(RentalDate) As Year, COUNT(*) as cnt
FROM t_loan l
GROUP BY fk_IdCustomer, Month, Year
HAVING cnt = (SELECT COUNT(*)
FROM t_loan l2
WHERE l2.fk_IdCustomer = l.fk_IdCustomer
GROUP BY MONTHNAME(RentalDate), YEAR(RentalDate)
ORDER BY COUNT(*) DESC
LIMIT 1
);
Note: If there are duplicates, you will get all matching values.
I searched around and found solutions, but they didn't work with MySQL because they used functions from other software.
I'm trying to show month-over-month growth for the current year (starting January), though knowing how to check within the past year might come in handy in the future as well.
What the "orders" table might look like:
+-----------+-------+
| Month | Sales |
+-----------+-------+
| 1-1-2017 | 3 |
| 1-5-2017 | 9 |
| 2-16-2017 | 10 |
| 2-16-2017 | 13 |
| 3-7-2017 | 25 |
| 4-29-2017 | 22 |
+-----------+-------+
What I want the query result to look like:
+----------+-------+--------+
| Month | Sales | Growth |
+----------+-------+--------+
| January | 12 | |
| February | 23 | 91.66% |
| March | 25 | 8.69% |
| April | 22 | -12% |
+----------+-------+--------+
Is there a simple way to do this?
You can do something like that:
SELECT
thisMonth.MonthOnly,
SUM(thisMonth.Sales) AS ThisMonthSales,
(SUM(thisMonth.Sales) / SUM(lastMonth.Sales) - 1) * 100 AS Growth
FROM
(
SELECT STR_TO_DATE(DATE_FORMAT(Month, '%Y%m01'), '%Y%m%d') AS MonthOnly,
SUM(Sales) AS Sales
FROM orders
GROUP BY DATE_FORMAT(Month, '%Y%m01')
) thisMonth
LEFT OUTER JOIN
(
SELECT STR_TO_DATE(DATE_FORMAT(DATE_ADD(Month, INTERVAL 1 MONTH), '%Y%m01'), '%Y%m%d') AS MonthOnly,
SUM(Sales) AS Sales
FROM orders
GROUP BY DATE_FORMAT(Month, '%Y%m01')
) lastMonth
ON thisMonth.MonthOnly = lastMonth.MonthOnly
GROUP BY thisMonth.MonthOnly
Let's say I have a table "calendar"
+------------+
| day_date |
+------------+
| 2015-01-01 |
| 2015-01-02 |
| 2015-01-03 |
| .......... |
| 2015-07-14 |
| 2015-07-15 |
+------------+
With this query I can select the WEEK (that I need)
SELECT WEEK(day_date,1) AS NUM_WEEK,
YEAR(day_date) AS YEAR,
STR_TO_DATE(CONCAT(YEAR(day_date),WEEK(day_date,1),' Monday'), '%X%V %W') AS date_start
FROM calendar
GROUP BY NUM_WEEK
And this is the result:
+----------+------+------------+
| NUM_WEEK | YEAR | date_start |
+----------+------+------------+
| 29 | 2015 | 2015-07-20 |
| 30 | 2015 | 2015-07-27 |
| 31 | 2015 | 2015-08-03 |
| 32 | 2015 | 2015-08-10 |
| 33 | 2015 | 2015-08-17 |
| 34 | 2015 | 2015-08-24 |
| 35 | 2015 | 2015-08-31 |
| 36 | 2015 | 2015-09-07 |
| 37 | 2015 | 2015-09-14 |
| 38 | 2015 | 2015-09-21 |
| 39 | 2015 | 2015-09-28 |
| 40 | 2015 | 2015-10-05 |
| 41 | 2015 | 2015-10-12 |
| 42 | 2015 | 2015-10-19 |
| 43 | 2015 | 2015-10-26 |
+----------+------+------------+
Now I have another table:
+----+------------+--------+---------------------+
| id | id_account | amount | date_transaction |
+----+------------+--------+---------------------+
| 1 | 283 | 150 | 2015-06-21 15:50:47 |
| 2 | 283 | 47.74 | 2015-07-23 15:55:44 |
| 3 | 281 | 21.55 | 2015-08-24 12:27:11 |
| 4 | 283 | 11.22 | 2015-08-25 10:00:54 |
+----+------------+--------+---------------------+
They are gaps in date.
With a similar query:
SELECT WEEK(date_transaction,1) AS NUM_WEEK,
YEAR(date_transaction) AS YEAR,
STR_TO_DATE(CONCAT(YEAR(date_transaction),WEEK(date_transaction,1),' Monday'), '%X%V %W')
AS date_start,
transaction.id_account,
SUM(amount) as total FROM transaction
INNER JOIN account ON account.id_account = transaction.id_account
WHERE amount > 0 AND transaction.id_account
IN ( SELECT id_account FROM account WHERE id_customer = 12 )
GROUP BY id_account, WEEK(date_transaction,1)
I obtain this result (probably data are not accurate, referring to previous tables, just to explain).
+----------+------+------------+-----------+----------+
| NUM_WEEK | YEAR | date_start | idAccount | total |
+----------+------+------------+-----------+----------+
| 29 | 2015 | 2015-07-20 | 281 | 22377.00 |
| 30 | 2015 | 2015-07-27 | 281 | 11550.00 |
| 32 | 2015 | 2015-08-04 | 281 | 4500.00 |
| 30 | 2015 | 2015-07-27 | 283 | 1500 |
+----------+------+------------+-----------+----------+
What I would, RIGHT (or LEFT) JOINING the two tables?
The min (and max) WEEK, so I can... (see 2)
Fill the gaps with missing WEEKS with NULL VALUES.
E.g., in a more complicated resultset:
+----------+------+------------+-----------+----------+
| NUM_WEEK | YEAR | date_start | idAccount | total |
+----------+------+------------+-----------+----------+
| 29 | 2015 | 2015-07-20 | 281 | 22377.00 |
| 30 | 2015 | 2015-07-27 | 281 | 11550.00 |
| 31 | 2015 | 2015-07-02 | 281 | NULL |
| 32 | 2015 | 2015-08-09 | 281 | 4500.00 |
| 29 | 2015 | 2015-08-09 | 283 | NULL |
| 30 | 2015 | 2015-07-16 | 283 | 1500 |
| 31 | 2015 | 2015-07-16 | 283 | NULL |
| 32 | 2015 | 2015-07-16 | 283 | NULL |
+----------+------+------------+-----------+----------+
Note, for example, that id=283 now has NULL at WEEK 29, 31 and 32, for example, like id=281 has NULL in WEEK 31.
I prepared also SQLFiddle here: http://sqlfiddle.com/#!9/a8fdc/3
Thank you very much.
I take a look on your question and i came up with this solution. Here is how your query could look like:
SELECT t1.NUM_WEEK, t1.`YEAR`, t1.date_start, t1.id_account, t2.total
FROM (SELECT c.NUM_WEEK, c.`YEAR`, c.date_start, a.id_account
FROM (SELECT WEEK(day_date,1) AS NUM_WEEK,
YEAR(day_date) AS `YEAR`,
STR_TO_DATE(CONCAT(YEAR(day_date),WEEK(day_date,1),' Monday'), '%X%V %W') AS date_start,
(SELECT GROUP_CONCAT(id_account) FROM account WHERE id_customer=12) AS accounts_id
FROM calendar
GROUP BY NUM_WEEK) c
INNER JOIN account a
ON FIND_IN_SET(a.id_account, c.accounts_id)
ORDER BY a.id_account, c.NUM_WEEK) t1
LEFT JOIN
(SELECT WEEK(t.date_transaction,1) AS NUM_WEEK,
YEAR(t.date_transaction) AS `YEAR`,
STR_TO_DATE(CONCAT(YEAR(t.date_transaction),WEEK(t.date_transaction,1),' Monday'), '%X%V %W') AS date_start,
t.id_account, SUM(t.amount) AS total
FROM `transaction` t
INNER JOIN account a
ON a.id_account = t.id_account
WHERE t.amount > 0 AND
t.id_account IN (SELECT id_account FROM account WHERE id_customer = 12)
GROUP BY id_account, WEEK(date_transaction,1)) t2
ON t1.NUM_WEEK = t2.NUM_WEEK AND t1.YEAR = t2.YEAR AND t1.id_account = t2.id_account;
Here is SQL Fiddle for that so you can check up result. Hope that is what are you looking for.
Little explanation:
First think i done is that I little modified your first query where you extract data from table calendar and add there one new column called accounts_id. That query now look's like this:
SELECT WEEK(day_date,1) AS NUM_WEEK,
YEAR(day_date) AS `YEAR`,
STR_TO_DATE(CONCAT(YEAR(day_date),WEEK(day_date,1),' Monday'), '%X%V %W') AS date_start,
(SELECT GROUP_CONCAT(id_account) FROM account WHERE id_customer=12) AS accounts_id
FROM calendar
GROUP BY NUM_WEEK
Please pay attention on this line in SELECT statement
(SELECT GROUP_CONCAT(id_account) FROM account WHERE id_customer=12) AS accounts_id
Note that when you select for specific customer you need to change customer ID in this line too!!!
Here is Fiddle so you can check result that this query produce.
This is necessary because we need to connect each week with each account to get desired result.
Next step is to extend previous query so we could separate accounts_id column (look result of previous query) so we could get row for each value in that column. Extended query look like this:
SELECT c.NUM_WEEK, c.`YEAR`, c.date_start, a.id_account
FROM (SELECT WEEK(day_date,1) AS NUM_WEEK,
YEAR(day_date) AS `YEAR`,
STR_TO_DATE(CONCAT(YEAR(day_date),WEEK(day_date,1),' Monday'), '%X%V %W') AS date_start,
(SELECT GROUP_CONCAT(id_account) FROM account WHERE id_customer=12) AS accounts_id
FROM calendar
GROUP BY NUM_WEEK) c
INNER JOIN account a
ON FIND_IN_SET(a.id_account, c.accounts_id)
ORDER BY a.id_account, c.NUM_WEEK
and output you can see in this Fiddle
After that all we need to do is to make left join between this query and query you already wrote in your question (last query).
There might be a better solution or even this one maybe can be improved a little, but I don't have much time now to deal with that and this is the first think that cross my mind...
GL!
P. S. pay attention when you use reserved word in MySQL like YEAR, TRANSACTION etc for column name (as column_name).. that can cause you a treble if have to use them in name of column or table use backquote () to mark them (asyear`)...
I have several queries which are similar and only differ in the conditional clause. I am trying to combine all the queries into a single one.
The first query looks like this:
First query:
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 |
The second query is very similar, only difference is in the conditional wikip_member=1 instead of admin.
Second query:
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 |
Essentially what I need the output to look like is like this:
Intended output:
+------------------------------------------+------+-------+--------+-------+
| 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 |
Which would be a combination of the queries. Basically getting the COUNT of the revisions for each month and showing a 0 when no revisions that matches the conditions are found for that month. I have tried a few methods such as UNION, SUBSELECT, but didn't have any success. The UNION didn't seem to work as intended and the SUBSELECT took 5 hours to run for only 128 rows.
UNION ATTEMPT:
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;
SUBSELECT ATTEMPT:
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;
Any help pointing me in the right direction would be greatly appreciated. This is for my final presentation which is due on Friday and this is the last query I need to wrap everything together.
In MySql (and many other SQL dialects), you can join to a subselect. This should perform better, as the engine will run the query once to produce a single result set, as opposed to running a subquery that is part of the select clause once per row of the initial results.
So, you'd try something like this:
select COALESCE(a.title, b.title),
COALESCE(a.year, b.year),
COALESCE(a.month, b.month),
COALESCE(a.Cadmin_edits, 0),
COALESCE(b.Wpart_edits, 0)
FROM (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) a
FULL JOIN (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) b
ON a.title = b.title AND a.year = b.year AND a.month = b.month
/*order the full results as you please*/
If this were Oracle or Sql Server, I would have recommended a couple of common table expressions for the subqueries, as they would allow you to remove the subquery from the main query, making the full query easier to understand.
I think you can just do WHERE userid!=0 AND(admin=1 OR wikip_member=1) if I got what you need correct.
SELECT pages.title,
YEAR(page_revisions.date) AS iYear,
MONTH(page_revisions.date) AS iMonth,
COUNT(revid) AS Cadmin_edits,
0 AS Wpart_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 pages.title, YEAR(page_revisions.date), MONTH(page_revisions.date)
UNION ALL
SELECT pages.title,
YEAR(page_revisions.date),
MONTH(page_revisions.date),
0,
COUNT(revid)
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 pages.title, YEAR(page_revisions.date), MONTH(page_revisions.date)
ORDER BY title, iYear, iMonth;