sql 本日\本周\本月统计
2015-07-09 14:44
435 查看
sql 本日\本周\本月统计
eg:1> selectasset_info_src_cd,count(*) from asset wheredatediff(week,asset_setup_dt,getdate())=0
2> group by asset_info_src_cd
eg:
select asset_info_src_cd,count (asset_info_src_cd) from asset wheredatepart(week,asset_setup_dt) =datepart(week,getdate()) group by asset_info_src_cd
eg: select asset_info_src_cd,count(*) from asset where asset_setup_dt >'20150706' and asset_setup_dt <=getdate() group by asset_info_src_cd
本月统计(MySQL)
select * from booking where month(booking_time) = month(curdate()) and year(booking_time) = year(curdate())
本周统计(MySQL)
select * from spf_booking where month(booking_time) = month(curdate()) and week(booking_time) = week(curdate())
[SQLServer]
表名为:tableName
时间字段名为:theDate
查询本月的记录
select * from tableName where DATEPART(mm, theDate) = DATEPART(mm, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
查询本周的记录
select * from tableName where DATEPART(wk, theDate) = DATEPART(wk, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
查询本季的记录
select * from tableName where DATEPART(qq, theDate) = DATEPART(qq, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
其中:GETDATE()是获得系统时间的函数。
如:
表:consume_record
字段:consume (money类型)
date (datetime类型)
请问怎么写四条sql语句分别按日,按周,按月,按季统计消费总量.
如:1月 1200元
2月 3400元
3月 2800元
--按日
select sum(consume),day([date]) from consume_record where year([date]) = '2006' group by day([date])
--按周quarter
select sum(consume),datename(week,[date]) from consume_record where year([date]) = '2006' group by datename(week,[date])
--按月
select sum(consume),month([date]) from consume_record where year([date]) = '2006' group by month([date])
--按季
select sum(consume),datename(quarter,[date]) from consume_record where year([date]) = '2006' group by datename(quarter,[date])
--指定日期你就看上面的例子变通下呀,无非就是一个聚合函数和Group by
select [date],sum(consume) from consume_record where [date] between '2006-06-01' and '2006-07-10' group by [date]
统计博客聚合用户点击次数
SELECT author, SUM(hits) AS hits
FROM infos
GROUP BY author
ORDER BY hits DESC
统计昨天的记录
SELECT *
FROM infos
WHERE (DATEDIFF(d, pubdate, GETDATE()) = 1)
统计本周的记录
SELECT *
FROM infos
WHERE (DATEPART(yy, pubdate) = DATEPART(yy, GETDATE())) AND (DATEPART(week,
pubdate - 1) = DATEPART(week, GETDATE()))
统计本月的记录:
SELECT *
FROM infos
WHERE (DATEPART(yy, pubdate) = DATEPART(yy, GETDATE())) AND (DATEPART([month],
pubdate - 1) = DATEPART([month], GETDATE()))
eg:
select asset_info_src_cd,count (asset_info_src_cd) from asset wheredatepart(week,asset_setup_dt) =datepart(week,getdate()) group by asset_info_src_cd
eg: select asset_info_src_cd,count(*) from asset where asset_setup_dt >'20150706' and asset_setup_dt <=getdate() group by asset_info_src_cd
相关文章推荐
- sql查询语句中like问号传参,占位符对不上
- MySQL修改root账号密码
- 解决pl/sql developer 中文字段显示乱码或无法用中文作查询条件的问题
- MySQL 初始化root 密码
- Redis 3.0 集群搭建
- Mongodb模糊查询
- 如何提升MySQL批量插入的效率
- Percona Xtrabackup快速备份MySQL
- Slave_SQL_Running: No mysql同步故障解决
- mysql 常用sql
- 深入剖析Redis主从复制
- SQL Developer 警告--无法安装某些模块
- 我要遵守11文章数据库设计指南
- Redis启动多端口、运行多实例
- SQL中char、varchar、nvarchar的区别
- SQLServer学习笔记系列1
- Postgresql psql文件执行与批处理
- Oracle错误IMP-00010: 不是有效的导出文件, 头部验证失败
- Oracle错误IMP-00010: 不是有效的导出文件, 头部验证失败 分类: Oracle 2015-07-09 13:56 20人阅读 评论(0) 收藏
- sql定期移植数据的存储过程