我有一個網站是鏡像交叉兩個子域。所以我有兩個分開的分析數據集。 我有以下表格:複雜(對我來說)MySQL日期匹配
|------------------------------|
| table_a |
|------------------------------|
| url | mod_date |
|------------------------------|
| /foo/index.html | 2009-10-24 |
| /bar/index.php | 2010-01-04 |
| /foo/bar.html | 2009-01-04 |
|------------------------------|
|-----------------------------------------|
| table_b |
|-----------------------------------------|
| url | views | access_date |
|-----------------------------------------|
| /foo/index.html | 35000 | 2009-12-01 |
| /foo/index.html | 20000 | 2010-02-01 |
| /bar/index.php | 35000 | 2010-01-01 |
| /bar/index.php | 15000 | 2011-01-01 |
|-----------------------------------------|
|-----------------------------------------|
| table_c |
|-----------------------------------------|
| url | views | access_date |
|-----------------------------------------|
| /foo/index.html | 35000 | 2009-10-01 |
| /foo/bar.html | 10000 | 2011-05-01 |
| /bar/index.php | 35000 | 2011-08-01 |
| /bar/index.php | 15000 | 2012-04-01 |
|-----------------------------------------|
我有以下查詢:
SELECT
a.url
,DATE_FORMAT(a.mod_date, '%d/%m/%Y') AS 'mod_date'
,DATE_FORMAT(MIN(b.access_date), '%d/%m/%Y') AS 'first_date'
,DATE_FORMAT(MAX(b.access_date), '%d/%m/%Y') AS 'last_date'
,SUM(ifnull(b.pages,0)) + SUM(ifnull(c.pages,0)) AS 'page_views'
,DATEDIFF(MAX(b.access_date),MIN(b.access_date)) AS 'days'
,ROUND(SUM(b.pages)/(DATEDIFF(MAX(b.access_date),MIN(b.access_date))/30.44)) AS 'b_mean_monthly_hits'
,ROUND(SUM(c.pages)/(DATEDIFF(MAX(c.access_date),MIN(c.access_date))/30.44)) AS 'a_mean_monthly_hits'
FROM
tabl_a a
LEFT JOIN
table_b b ON b.url = a.url
LEFT JOIN
table_c c ON c.url = a.url
GROUP BY a.url
HAVING ROUND(SUM(b.pages)/(DATEDIFF(MAX(b.access_date),MIN(b.access_date))/30.44)) < 5
AND ROUND(SUM(c.pages)/(DATEDIFF(MAX(c.access_date),MIN(c.access_date))/30.44)) < 5
;
我在尋找的結果是:
|------------------------------------------------------------------------------------------|
| results |
|------------------------------------------------------------------------------------------|
| url | mod_date | first_date | last_date | page_views | avg_monthly_hits |
|------------------------------------------------------------------------------------------|
| /foo/index.html | 2009-10-24 | 2009-10-01 | 2010-02-01 | 90000 | 22273 |
| /bar/index.php | 2010-01-04 | 2010-01-01 | 2012-04-01 | 85000 | 3275 |
| /foo/bar.html | 2009-01-04 | 2011-05-01 | 2011-06-01 | 10000 | 9819 |
|------------------------------------------------------------------------------------------|
凡'avg_monthly_hits'是總和b.views and c.views天數除以(如「PAGE_VIEWS」)(不知道如何讓個月)從表-B或table_c由30.44分(平均數量最古老和最新ACCESS_DATE之間一個月中的天數)。
我希望我已經完全解釋了自己。 :)
可以提供http://sqlfiddle.com –
架構和你能解釋no_of_dates? –