sql server 按日期时间年,月,日,时分组查询
select datepart(YEAR,CreateDateTime) yyyy,datepart(month,CreateDateTime) mm,datepart(day,CreateDateTime) dd ,datepart(hour,CreateDateTime) hh
from Symptom
GROUP BY datepart(YEAR,CreateDateTime),datepart(month,CreateDateTime),datepart(day,CreateDateTime),datepart(hour,CreateDateTime)
sql server 按日期时间年,月,日,时分组查询,并统计数量
select datepart(YEAR,CreateDateTime) yyyy,datepart(month,CreateDateTime) mm,datepart(day,CreateDateTime) dd ,datepart(hour,CreateDateTime) hh,COUNT(SymptomId) count
from Symptom
GROUP BY datepart(YEAR,CreateDateTime),datepart(month,CreateDateTime),datepart(day,CreateDateTime),datepart(hour,CreateDateTime)
SqlServer日期时间函数,最全
https://blog.csdn.net/cplvfx/article/details/108539989
原文地址:https://www.cnblogs.com/haosit/p/10766954.html
内连接(inner join)。左连接(left join),以左边为基准,全部查询右边没有匹配的值显示为空。右链接(right join),和左连接相反,以右边为基准左边表的字段为空。全链接(full outer join)左右全部连接没有对应值得字段为空。交叉链接(coress join)笛卡尔积
-- inner join 显示
select * from Users
inner join Wallets on Users.ID = Wallets.User_ID
where Money > 30000
-- inner join 隐式
select * from Wallets,Users
where Wallets.User_ID = Users.ID and Money > 10000
-- left join
select * from Users
left join Wallets on Users.ID = Wallets.User_ID
-- right join
select * from Wallets
right join Users on Users.ID = Wallets.User_ID
-- full outer join
select * from Users
full outer join Wallets on Users.ID = Wallets.User_ID
where User_ID is null or Users.ID is null
-- cross join
select * from Users
cross join Wallets where Users.ID = 3
--查询钱包里金额大于30000
select * from Users where exists (select User_ID from Wallets where Money > 30000 and User_ID = Users.ID)
select * from Users where ID in (select User_ID from Wallets where Money > 30000)
1. 根据日期分组
根据convert 的第一个参数,设置结果的长度达到更具年月分组或年月日分组等
2017-09 长度 7 varchar(7)
2017-09-01 长度10 varchar(10)
-- 使用convert() 函数方式
select convert(varchar(7),CreatTime,120)日期,COUNT(*) 次数,sum(Money)总数 from Orders
group by convert(varchar(7),CreatTime,120)
-- 先查出对应的年月日等时间,再根据查询出的字段分组
select YEAR(CreatTime) 年,month(CreatTime) 月,COUNT(*) 次数,sum(Money)总数 from Orders
group by month(CreatTime),YEAR(CreatTime)
order by 年,月 asc
2. 根据划分时间段进行分组(每三个小时一组)
dataname:返回代表指定日期的指定日期部分的字符串。一个参数代表返回的格式,第二个参数为具体值dw:星期几,qq, q:季度,yy, yyyy:年,mm,m:月,dd、d:日,hh:小时,mi, n:分钟 ss、s:秒,ms:毫秒
select t.number 记录数,
CONVERT(varchar(2),t.timeGroup * 3)+':00~'+ CONVERT(varchar(2),t.timeGroup * 3 + 2)+':59' 时间段
from
(select datename(hh,CreateTime) /3 timeGroup,COUNT(1) number from ClassOptions
group by datename(hh,CreateTime) / 3) t
原文地址:https://www.cnblogs.com/yechangzhong-826217795/p/13408549.html
例1:
select Min(VisitTime),count(*) as Num from online group by datediff(day,VisitTime,getdate())
例2:
select count(*),Min(PayTime) from LimitBidBills where PayTime>'2020-07-25' and Status='已确认'
group by datediff(day,PayTime,getdate())
order by Min(PayTime) asc
例3:
select count(*),Min(PayTime) from LimitBidBills join LimitBidMail on LimitBidBills.InfoID = LimitBidMail.InfoID where PayTime>'2020-06-20' and PayTime<'2020-07-5' and Status='已确认'
group by datediff(day,PayTime,getdate())
order by Min(PayTime) asc
原文地址:https://www.cnblogs.com/ding5688/p/11235587.html
--按照月份统计
select count(id) cnt,datepart(mm,time) [Month]
from [table]
where [time] between '2007/09/08 09:10:43' and '2007/10/09 04:32:37'
group by datepart(mm,time)
--按照日统计
select count(id) cnt,datepart(dd,time) [Day]
from [table]
where time between '2007/09/08 09:10:43' and '2007/10/09 04:32:37'
group by datepart(dd,time)
--按照小时统计
select count(id) cnt,datepart(hh,time) [Hour]
from [table]
where [time] between '2007/09/08 09:10:43' and '2007/10/09 04:32:37'
group by datepart(hh,time)
1、每年
select year(ordertime) AS '年',
sum(Total) '销售合计'
from order_list
group by year(ordertime)
2、每月
select year(ordertime) '年',
month(ordertime) '月',
sum(Total) '销售合计'
from order_list
group by year(ordertime),
month(ordertime)
3、每日
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)
另外,每月(年、日)的记录条数
select year(ordertime) '年',
month(ordertime) '月',
count(*) '销售记录'
from order_list
group by year(ordertime),
month(ordertime)