sql 數據按天按月進行分組統計語句
sql 數據按天按月進行分組統計語句
/*
數據庫教程orders表中有一個“total”和一個“createtime”字段,現要當月每一天的total的總,如這個月有31天,就要得到31條total的總和
*/
$sql ="select sum(total),convert(char(10),createtime,120) createtime
from tb
group by convert(char(10),createtime,120)
order by sum(total) desc
";
$sql2 ="select sum(total) 總合, convert(varchar(7),createtime,120) as 月
from orders
group by convert(varchar(7),createtime,120)";
$sql3 = "select sum(total) as 總和 ,convert(char(10),createtime,120) createtime
from tb
where datepart(month,createtime)=datepart(month,getdate())
group by convert(char(10),createtime,120)
";
$sql4 =" select
convert(datetime, floor(convert(float, createtime))), sum(total)
from
orders
group by
floor(convert(float, createtime))
";
/*
上面的四種mssql sql 數據按天按月進行分組統計語句各有各的優點,各位可以試一下。
*/