嗨,请观看下面的图片链接,了解桌面结构。 http://i31.tinypic.com/2v0zw4o.jpg
我们必须以下面的布局格式打印它......就像d相同的日期差异区域一样,总计数....
Date CentroOeste Nordeste Norte RJ_ES_MG SaoPaulo Sul 7/6/2010 233 204 154 130 128 99 7/7/2010 0 0 0 0 3 0 7/8/2010 0 0 0 0 0 13 7/16/2010 0 0 0 300 0 0 8/6/2010 0 0 0 0 3 43 Total 233 204 154 430 134 155
我怎样才能做到这一点?请提供存储过程或查询以获得所需的输出。
答案 0 :(得分:0)
试试这个(未经测试):
SELECT
DATE_FORMAT(unique_timestamps.statstimestamp, '%d/%m/%Y') AS 'Date',
(SELECT SUM(count) FROM table WHERE region = 'CentroOeste' AND statstimestamp = unique_timestamps.statstimestamp) AS 'CentroOeste',
(SELECT SUM(count) FROM table WHERE region = 'NordOeste' AND statstimestamp = unique_timestamps.statstimestamp) AS 'NordOeste',
(SELECT SUM(count) FROM table WHERE region = 'Norte' AND statstimestamp = unique_timestamps.statstimestamp) AS 'Norte',
(SELECT SUM(count) FROM table WHERE region = 'RJ_ES_MG' AND statstimestamp = unique_timestamps.statstimestamp) AS 'RJ_ES_MG',
(SELECT SUM(count) FROM table WHERE region = 'SaoPaulo' AND statstimestamp = unique_timestamps.statstimestamp) AS 'SaoPaulo',
(SELECT SUM(count) FROM table WHERE region = 'Sul' AND statstimestamp = unique_timestamps.statstimestamp) AS 'Sul'
FROM
(SELECT DISTINCT statstimestamp FROM table) unique_timestamps;
答案 1 :(得分:0)
SELECT
DATE_FORMAT(statstimestamp, '%d/%m/%Y') AS 'Date',
SUM((region = 'CentroOeste')*count) AS 'CentroOeste',
SUM((region = 'NordOeste')*count) AS 'NordOeste',
SUM((region = 'Norte')*count) AS 'Norte',
SUM((region = 'RJ_ES_MG')*count) AS 'RJ_ES_MG',
SUM((region = 'SaoPaulo')*count) AS 'SaoPaulo',
SUM((region = 'Sul')*count) AS 'Sul',
SUM(count) as 'SubTotal'
FROM
table
group by 1
union all
SELECT
'Total',
SUM((region = 'CentroOeste')*count) AS 'CentroOeste',
SUM((region = 'NordOeste')*count) AS 'NordOeste',
SUM((region = 'Norte')*count) AS 'Norte',
SUM((region = 'RJ_ES_MG')*count) AS 'RJ_ES_MG',
SUM((region = 'SaoPaulo')*count) AS 'SaoPaulo',
SUM((region = 'Sul')*count) AS 'Sul',
SUM(count)
FROM
table;
我添加了另一个专栏“小计”,以便按天计算,也可以在最后一行提供总计。
更新我在第一次发布查询后做了一些更正,它在类似的结构上进行了测试,它必须工作。