2013-07-10 69 views
0

我有下面的格式有數據表:MySQL-加入基於列的數據

+---------------------+--------------+-------------------+-------------------+ 
| date    | downloadtime | clientcountrycode | clientcountryname | 
+---------------------+--------------+-------------------+-------------------+ 
| 2013-07-10 10:44:29 |   2 | USA    | United States  | 
| 2013-07-10 10:44:25 |   4 | USA    | United States  | 
| 2013-07-10 10:44:21 |   7 | USA    | United States  | 
| 2013-07-10 10:44:16 |   2 | USA    | United States  | 
| 2013-07-10 10:44:10 |   3 | USA    | United States  | 
+---------------------+--------------+-------------------+-------------------+ 

我需要通過查詢該表準備CSV文件。該csv文件應該是以下格式:

clientcountryname,clientcountrycode,2013-07-05,2013-07-06,2013-07-8... 
United States,USA,22,23,24 

所以,基本上我需要得到每個國家每天的平均下載時間。 我有一個查詢,這將給我AVG(downloadtime)某一天:

SELECT clientcountryname,clientcountrycode, avg(downloadtime), FROM tb_npp where date(date) = '2013-07-10' group by clientcountrycode; 

+---------------------------------------+-------------------+-------------------+ 
| clientcountryname      | clientcountrycode | avg(downloadtime) | 
+---------------------------------------+-------------------+-------------------+ 
| Anonymous Proxy      | A1    |   118.0833 | 
| Satellite Provider     | A2    |   978.5000 | 
| Aruba         | ABW    |   31.8462 | 

我的問題是:有沒有在SQL中組基於這是目前在我的數據庫日期列名的方法嗎?

回答

1

如果我理解你正確的問題,你就應該能夠到組的日期還有:

SELECT clientcountryname,clientcountrycode,Date, avg(downloadtime), 
FROM tb_npp 
GROUP BY clientcountrycode,clientCountryCode,Date; 
+0

感謝,但沒有我一直在尋找這樣一個完整的外部基於日期列數據加入 –