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、
=================================================
SQL Server常用SQL集合的更多相关文章
- SQL Server 常用SQL
--查询所有表 select * from sysobjects where xtype='u' ORDER BY name ASC
- sql server 常用sql语句
--删除约束 alter table productInfo drop constraint 约束名称 --删除列alter table productInfo drop column 列名 --添加 ...
- [SQL Server] 常用sql脚本
1.添加表 GO IF NOT EXISTS(SELECT * FROM sys.tables WHERE name='table_name') BEGIN CREATE TABLE [dbo].[t ...
- sql server 常用的系统存储过程
系统存储过程 说明 sp_databases 列出服务上的所有数据库 sp_helpdb 报告有关指定数据库或所有数据库的信息 sp_renamedb 更改数据库的名称 sp_tables 返回当 ...
- SQL Server中的集合运算: UNION, EXCEPT和INTERSECT
SQL Server中的集合运算包括UNION(合并),EXCEPT(差集)和INTERSECT(相交)三种. 集合运算的基本使用 1.UNION(合并两个查询结果集,隐式DINSTINCT,删除重复 ...
- SQL SERVER常用语法记录
用于记录SQL SERVER常用语法,以及内置函数. 以下语句包含: WITH 临时表语法 ROW_NUMBER()内置函数,我一般主要是用来分页.针对于查出来的所有数据做一个数字排序 分页的BETW ...
- SQL SERVER常用语法汇总
阅读目录 一.SQL分类 二.基础语句 三.sql技巧 四.(MS SQL Server)SQL语句导入导出大全 回到目录 一.SQL分类 DDL—数据定义语言(CREATE,ALTER,DROP,D ...
- sql server常用性能计数器
https://blog.csdn.net/kk185800961/article/details/52462913?utm_source=blogxgwz5 https://blog.csdn.ne ...
- SQL Server经典sql语句大全(转)
一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备份数 ...
随机推荐
- POJ - 2528 Mayor's posters(dfs+分治)
POJ - 2528 Mayor's posters 思路:分治思想. 代码: #include<iostream> #include<cstdio> #include< ...
- 推荐一款基于Angular实现的企业级中后台前端/设计解决方案脚手架
ng-alain 是一个企业级中后台前端/设计解决方案脚手架,我们秉承 Ant Design 的设计价值观,目标也非常简单,希望在Angular上面开发企业后台更简单.更快速.随着『设计者』的不断反馈 ...
- CF576E Painting Edges
首先,有一个很暴力的nk的做法,就是对每种颜色分别开棵lct来维护. 实际上,有复杂度与k无关的做法. 感觉和bzoj4025二分图那个题的区别就在于这个题是边dfs线段树边拆分区间.
- 『OpenCV3』Harris角点特征_API调用及python手动实现
一.OpenCV接口调用示意 介绍了OpenCV3中提取图像角点特征的函数: # coding=utf- import cv2 import numpy as np '''Harris算法角点特征提取 ...
- fzu1901 kmp
For each prefix with length P of a given string S,if S[i]=S[i+P] for i in [0..SIZE(S)-p-1], then the ...
- zk maxClientCnxns参数
在zk模板配置文件中有: # the maximum number of client connections. # increase this if you need to handle more ...
- 简话Angular 05 Angular表单验证
一句话: 可以使用所有html5表单验证功能,同时Angular还增强了部分验证,支持动态验证 1. 上源码 <div ng-controller="ExampleController ...
- spring cloud学习(七)Spring Cloud Config(续)
Spring Cloud Config(续) 个人参考项目 个人博客 : https://zggdczfr.cn/ 个人参考项目 : (整合到上一个案例中)https://github.com/Fun ...
- sgu 129 Inheritance 凸包,线段交点,计算几何 难度:2
129. Inheritance time limit per test: 0.25 sec. memory limit per test: 4096 KB The old King decided ...
- 跟我一起学习ASP.NET 4.5 MVC4.0(四)
前几个文章中介绍了一些关于MVC4.0的东东,今天我们来看一下登陆验证,也可以说是权限验证,即AuthorizeAttribute.这个可以使用在控制器Controller上,也可以使用在Action ...