SQL Server常用SQL集合

 

 

================================================

1、SQL查询一年之内的数据记录

select * from 表名 where  CreateDate<GETDATE() and

CreateDate>DATEADD(yy, -1, GETDATE())

2、--查询当天记录:

 

select * from info where DateDiff(dd,datetime,getdate())=0 

 

3、--查询24小时内的记录:

 

select * from info where DateDiff(hh,datetime,getDate())<=24 

 

5、--查询本周记录

select * from info where datediff(week,datetime,getdate())=0

 

6、--查询本月记录

 

select * from info where datediff(month,datetime,getdate())=0

 

--info为表名,datetime为数据库中的日期字段

 

DATEDIFF 函数:

语法:


DATEDIFF ( datepart , startdate , enddate )

 

SQL语句统计每天、每月、每年的 数据

 

7、统计每年

select year(ordertime) AS '',
sum(Total) '销售合计'
from order_list
group by year(ordertime)

 

8、统计每月

select year(ordertime) '',
month(ordertime) '',
sum(Total) '销售合计'
from order_list
group by year(ordertime),
month(ordertime)

 

9、统计每日

select year(ordertime) '',
month(ordertime) '',
day(ordertime) '',
sum(Total) '销售合计'
from order_list
group by year(ordertime),
month(ordertime),
day(ordertime)

另外也可以这样:

select convert(char(8),ordertime,112) dt,
sum(Total) '销售合计'
from order_list
group by convert(char(8),ordertime,112)

 

10、每月(年、日)的记录条数

select year(ordertime) '',
month(ordertime) '',
count(*) '销售记录'
from order_list
group by year(ordertime),
month(ordertime)

 

11、

12、

13、

14、

15、

16、

 

 

 

 

=================================================

posted @ 2018-10-17 10:40  DotNet码农  阅读(700)  评论(0编辑  收藏  举报
/*粒子线条,鼠标移动会以鼠标为中心吸附的特效*/