我有一个表格,其中包含以下格式的数据:
+---------------------+--------------+-------------------+-------------------+
| 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(下载时间):
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中是否有一种方法可以根据我的数据库中存在的日期对列名进行分组?
答案 0 :(得分:1)
如果我理解你的问题是正确的,你也应该能够按日期分组:
SELECT clientcountryname,clientcountrycode,Date, avg(downloadtime),
FROM tb_npp
GROUP BY clientcountrycode,clientCountryCode,Date;