sql语句怎么计算一个月

sql语句怎么计算一个月,第1张

先把年月取出来,分下组,求下每月的总数,然后对年月总数用where过滤一下就行了。

select year, month, Count

from (select to_char(to_date(tymd), 'yyyy') as year,

to_char(to_date(tymd), 'mm') as month,

count() as Count

from tablename t

group by to_char(to_date(tymd), 'yyyy'),

to_char(to_date(tymd), 'mm')

order by year, month)

where (month in (1, 3, 5, 7, 8, 10, 12) and count = 31)

or (month in (4, 6, 9, 11) and count = 30)

or (month = 2 and mod(year, 4) = 0 and count = 29)

or (month = 2 and mod(year, 4) <> 0 and count = 28)

找出当前是哪个月用  dataprat

创建一个FOR循环,一个个来扫描,扫描时用数据列占用日期的部份COUNT()就可以了

select @i, count() where @ i=datepart(mm,dd)

SELECT date_format(crtime,'%Y-%m') as qq ,

sum(order_logquan mersprice) as profit,

count(orderid) as Quant

FROM order_log

left outer join mers

on order_logtb_id = merstb_id

group by qq

举例中 我是两张表,一张表是销售记录order_log

另一张表是 商品价格mers

所以用了联合查询,如果你是同一张表就更简单了 不用联合查询

--计算收费员当月的总数

select 收费员,sum(钱) as 总数

from 收费表

where convert(varchar(10),收费日期,121)=convert(varchar(10),getdata(),121)

group by 收费员

select 部门名称 ,

case when substring(convert(Varchar(100),时间,112),5,2)='01' then count(部门名称) as 1月

case when substring(convert(Varchar(100),时间,112),5,2)='02' then count(部门名称) as 2月,

case when substring(convert(Varchar(100),时间,112),5,2)='03' then count(部门名称) as 3月,

from 第一个表名 group by 部门名称

以上就是关于sql语句怎么计算一个月全部的内容,包括:sql语句怎么计算一个月、sql怎么统计今年每个月的数据条数、怎么用sql语句返回近一年中每月的数据统计等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9458083.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-28
下一篇2023-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存