SELECT YMD, sum(IF(H = "00", count, 0)) DIV sum(IF(H = "00", 1, 0)) AS h00, sum(IF(H = "01", count, 0)) DIV sum(IF(H = "01", 1, 0)) AS h01, sum(IF(H = "02", count, 0)) DIV sum(IF(H = "02", 1, 0)) AS h02, sum(IF(H = "03", count, 0)) DIV sum(IF(H = "03", 1, 0)) AS h03, sum(IF(H = "04", count, 0)) DIV sum(IF(H = "04", 1, 0)) AS h04, sum(IF(H = "05", count, 0)) DIV sum(IF(H = "05", 1, 0)) AS h05, sum(IF(H = "06", count, 0)) DIV sum(IF(H = "06", 1, 0)) AS h06, sum(IF(H = "07", count, 0)) DIV sum(IF(H = "07", 1, 0)) AS h07, sum(IF(H = "08", count, 0)) DIV sum(IF(H = "08", 1, 0)) AS h08, sum(IF(H = "09", count, 0)) DIV sum(IF(H = "09", 1, 0)) AS h09, sum(IF(H = "10", count, 0)) DIV sum(IF(H = "10", 1, 0)) AS h10, sum(IF(H = "11", count, 0)) DIV sum(IF(H = "11", 1, 0)) AS h11, sum(IF(H = "12", count, 0)) DIV sum(IF(H = "12", 1, 0)) AS h12, sum(IF(H = "13", count, 0)) DIV sum(IF(H = "13", 1, 0)) AS h13, sum(IF(H = "14", count, 0)) DIV sum(IF(H = "14", 1, 0)) AS h14, sum(IF(H = "15", count, 0)) DIV sum(IF(H = "15", 1, 0)) AS h15, sum(IF(H = "16", count, 0)) DIV sum(IF(H = "16", 1, 0)) AS h16, sum(IF(H = "17", count, 0)) DIV sum(IF(H = "17", 1, 0)) AS h17, sum(IF(H = "18", count, 0)) DIV sum(IF(H = "18", 1, 0)) AS h18, sum(IF(H = "19", count, 0)) DIV sum(IF(H = "19", 1, 0)) AS h19, sum(IF(H = "20", count, 0)) DIV sum(IF(H = "20", 1, 0)) AS h20, sum(IF(H = "21", count, 0)) DIV sum(IF(H = "21", 1, 0)) AS h21, sum(IF(H = "22", count, 0)) DIV sum(IF(H = "22", 1, 0)) AS h22, sum(IF(H = "23", count, 0)) DIV sum(IF(H = "23", 1, 0)) AS h23 FROM ( SELECT ID, count, DATE_FORMAT(AddTime, "%H") AS H, DATE_FORMAT(AddTime, "%Y-%m%-%d") AS YMD FROM allonlinecount ) t GROUP BY YMD ;
这个是本人本人写的,查询的每小时的平均值,每天的平均值,跟最高以及记录时间,最低值 以及记录时间,这个sql 语句要怎么样修改
目前的查询结果为:
在这里表里再增加列:
mincount mintime maxcount mantime dayavgcount
最低值 最低值记录时间 最高值 最高值记录时间 当日的平均值
这样复杂的语句要咋写,创建视图的话 要咋整?跪求高手!
表结构:
解决方案
100
SELECT YMD, sum(IF(H = "00", count, 0)) DIV sum(IF(H = "00", 1, 0)) AS h00, sum(IF(H = "01", count, 0)) DIV sum(IF(H = "01", 1, 0)) AS h01, sum(IF(H = "02", count, 0)) DIV sum(IF(H = "02", 1, 0)) AS h02, sum(IF(H = "03", count, 0)) DIV sum(IF(H = "03", 1, 0)) AS h03, sum(IF(H = "04", count, 0)) DIV sum(IF(H = "04", 1, 0)) AS h04, sum(IF(H = "05", count, 0)) DIV sum(IF(H = "05", 1, 0)) AS h05, sum(IF(H = "06", count, 0)) DIV sum(IF(H = "06", 1, 0)) AS h06, sum(IF(H = "07", count, 0)) DIV sum(IF(H = "07", 1, 0)) AS h07, sum(IF(H = "08", count, 0)) DIV sum(IF(H = "08", 1, 0)) AS h08, sum(IF(H = "09", count, 0)) DIV sum(IF(H = "09", 1, 0)) AS h09, sum(IF(H = "10", count, 0)) DIV sum(IF(H = "10", 1, 0)) AS h10, sum(IF(H = "11", count, 0)) DIV sum(IF(H = "11", 1, 0)) AS h11, sum(IF(H = "12", count, 0)) DIV sum(IF(H = "12", 1, 0)) AS h12, sum(IF(H = "13", count, 0)) DIV sum(IF(H = "13", 1, 0)) AS h13, sum(IF(H = "14", count, 0)) DIV sum(IF(H = "14", 1, 0)) AS h14, sum(IF(H = "15", count, 0)) DIV sum(IF(H = "15", 1, 0)) AS h15, sum(IF(H = "16", count, 0)) DIV sum(IF(H = "16", 1, 0)) AS h16, sum(IF(H = "17", count, 0)) DIV sum(IF(H = "17", 1, 0)) AS h17, sum(IF(H = "18", count, 0)) DIV sum(IF(H = "18", 1, 0)) AS h18, sum(IF(H = "19", count, 0)) DIV sum(IF(H = "19", 1, 0)) AS h19, sum(IF(H = "20", count, 0)) DIV sum(IF(H = "20", 1, 0)) AS h20, sum(IF(H = "21", count, 0)) DIV sum(IF(H = "21", 1, 0)) AS h21, sum(IF(H = "22", count, 0)) DIV sum(IF(H = "22", 1, 0)) AS h22, sum(IF(H = "23", count, 0)) DIV sum(IF(H = "23", 1, 0)) AS h23, (select count from allonlinecount where DATE_FORMAT(AddTime, "%Y-%m%-%d")=YMD order by `count` desc limit 1) as mincount , (select AddTime from allonlinecount where DATE_FORMAT(AddTime, "%Y-%m%-%d")=YMD order by `count` desc limit 1) as mintime , (select count from allonlinecount where DATE_FORMAT(AddTime, "%Y-%m%-%d")=YMD order by `count` limit 1) as maxcount , (select AddTime from allonlinecount where DATE_FORMAT(AddTime, "%Y-%m%-%d")=YMD order by `count` limit 1) as mantime , (select avg(count) from allonlinecount where DATE_FORMAT(AddTime, "%Y-%m%-%d")=YMD ) as dayavgcount FROM ( SELECT ID, count, DATE_FORMAT(AddTime, "%H") AS H, DATE_FORMAT(AddTime, "%Y-%m%-%d") AS YMD FROM allonlinecount ) t GROUP BY YMD ;