当前位置:首页 » 编程语言 » 按日期分组的sql
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

按日期分组的sql

发布时间: 2023-01-01 02:26:35

sql server 对日期每三天进行分组

1、首先需要一个能生成日期范围的表值函数

/*
生成一个日期范围,如2014.01、2014.02...
@Increment步进值
@StartDate开始日期
@EndDate结束日期
----------------------
@SelectedRange返回值名称(多语句表值函数的返回值必须存放在返回值变量中)
返回表的字段和类型:
iDateDATETIME,
iYearchar(4),
iMonthchar(2),
iYMCHAR(7)
*/

CREATEFUNCTION[dbo].[DateRange]
(
@Incrementchar(1)='m',
@StartDateDATE='20140101',
@EndDateDATE=null
)

RETURNS@SelectedRangeTABLE
(
iDateDATE,
iYearchar(4),
iMonthchar(2),
iYMCHAR(7)
)
AS
BEGIN
/*.
ThefunctionGETDATE(),soitisnotaconstantexpression.
*/
--IF@EndDateisnull
--SET@EndDate=getdate()
--or
SET@EndDate=Coalesce(@EndDate,GetDate())
;WITHcteRange(DateRange)AS
(
SELECTCAST(@StartDateASDATE)
UNIONALL
SELECT
CASE
WHEN@Increment='d'THENDATEADD(dd,1,DateRange)
WHEN@Increment='w'THENDATEADD(ww,1,DateRange)
WHEN@Increment='m'THENDATEADD(mm,1,DateRange)
END
FROMcteRange
WHEREDateRange<=
CASE
WHEN@Increment='d'THENDATEADD(dd,-1,@EndDate)
WHEN@Increment='w'THENDATEADD(ww,-1,@EndDate)
WHEN@Increment='m'THENDATEADD(mm,-1,@EndDate)
END
)
INSERTINTO@SelectedRange(iDate,iYear,iMonth,iYM)
SELECTDateRange,YEAR(DateRange)[Year],MONTH(DateRange)[Month],CONVERT(VARCHAR(7),DateRange,102)[YM]
FROMcteRange
OPTION(MAXRECURSION0);
RETURN
END

2、使用示例:

SELECT*FROMDateRange('d','2018-01-01','2018-06-01')

4、使用上面的查询去关联带值的表,然后进行SUM即可实现

⑵ sql 按时间分组排序写法

数据库软件不支持这样的输出方法,这个是报表软件干的事情,如果想随意输出样式,那要报表软件干嘛,开发报表软件就是为了解决这个事情的。

⑶ SQL: 一般情况按年分组,特殊年份按指定日期分组,SELECT语句怎么写

如果只是一年的话还好说,如果是三四年那就麻烦了,但是如果每一年都要这样还反而简单了,最怕的就是某些特殊要求,那样不好写。
我写一个一年的,写一个全部的。
这是一年的,可能casewhen不能这么直接放在groupby的后面,那么可以先这么改造一下表,然后外面在套一层select就可以实现了。
select case when year(日期)=2017 and month(日期)<5 then '2017-05-01以前'
when year(日期)=2017 and month(日期)>4 then '2017-05-01至年底' else year(日期) end
年份,count(*) from table group by
(case when year(日期)=2017 and month(日期)<5 then '2017-05-01以前'
when year(日期)=2017 and month(日期)>4 then '2017-05-01至年底' else year(日期) end)
全部的
select concat(year(日期),case when month(日期)<5 then 1 else 2 end) 组合字段,count(*) from table group by concat(year(日期),case when month(日期)<5 then 1 else 2 end)
其实是一样的,就是把原表改了一下,只是这两种情况是比较好改的,如果有三年或者四年是需要进行这样的分组,那就麻烦了。如果有这种情况,建议在表中加一个标示字段,那样就能简单一些了。

⑷ SQL根据时间分组问题

在分组的时候使用得到日期年份的函数year()进行分组就可以了.
select year(aghqjtsj01) as f_log_time,count(1) as counts from t_handleresou_log group by year(f_log_time)

⑸ SQL 按日期时间分组语句的写法

对,sql对DateTime类型数据操作时都会精确到毫秒,而用了CONVERT(varchar, in_date, 120)后会转换成yyyy-mm-dd hh:mi:ss(24小时制)格式

⑹ SQL中怎样对datetime数据类型按日期分组

转换后如果日期列是datetime的话这样写:
select cast(convert(varchar(10),日期,120) as datetime),金额 from 表

转换后如果日期列为varchar型的话这样写:
select convert(varchar(10),日期,120),金额 from 表

以上,希望对你有所帮助!

⑺ 时间段分组的SQL语句请教

select case when convert(char(2),lTime,108)>=0 and convert(char(2),lTime,108)<=1
then '00:00-01:00'
when convert(char(2),lTime,108)>=1 and convert(char(2),lTime,108)<=2
then '01:00-02:00'
when convert(char(2),lTime,108)>=2 and convert(char(2),lTime,108)<=3
then '02:00-03:00'
when convert(char(2),lTime,108)>=3 and convert(char(2),lTime,108)<=4
then '03:00-04:00'
when convert(char(2),lTime,108)>=4 and convert(char(2),lTime,108)<=5
then '04:00-05:00'
when convert(char(2),lTime,108)>=5 and convert(char(2),lTime,108)<=6
then '05:00-06:00'
when convert(char(2),lTime,108)>=6 and convert(char(2),lTime,108)<=7
then '06:00-07:00'
when convert(char(2),lTime,108)>=7 and convert(char(2),lTime,108)<=8
then '07:00-08:00'
when convert(char(2),lTime,108)>=8 and convert(char(2),lTime,108)<=9
then '08:00-09:00'
when convert(char(2),lTime,108)>=9 and convert(char(2),lTime,108)<=10
then '09:00-10:00'
when convert(char(2),lTime,108)>=10 and convert(char(2),lTime,108)<=11
then '10:00-11:00'
when convert(char(2),lTime,108)>=11 and convert(char(2),lTime,108)<=12
then '11:00-12:00'
when convert(char(2),lTime,108)>=12 and convert(char(2),lTime,108)<=13
then '12:00-13:00'
when convert(char(2),lTime,108)>=13 and convert(char(2),lTime,108)<=14
then '13:00-14:00'
when convert(char(2),lTime,108)>=14 and convert(char(2),lTime,108)<=15
then '14:00-15:00'
when convert(char(2),lTime,108)>=15 and convert(char(2),lTime,108)<=16
then '15:00-16:00'
when convert(char(2),lTime,108)>=16 and convert(char(2),lTime,108)<=17
then '16:00-17:00'
when convert(char(2),lTime,108)>=17 and convert(char(2),lTime,108)<=18
then '17:00-18:00'
when convert(char(2),lTime,108)>=18 and convert(char(2),lTime,108)<=19
then '18:00-19:00'
when convert(char(2),lTime,108)>=19 and convert(char(2),lTime,108)<=20
then '19:00-20:00'
when convert(char(2),lTime,108)>=20 and convert(char(2),lTime,108)<=21
then '20:00-21:00'
when convert(char(2),lTime,108)>=21 and convert(char(2),lTime,108)<=22
then '21:00-22:00'
when convert(char(2),lTime,108)>=22 and convert(char(2),lTime,108)<=23
then '22:00-23:00'
when convert(char(2),lTime,108)>=23 and convert(char(2),lTime,108)<=24
then '23:00-00:00'
end as 时间段,sum(lNum)合计
from listtable group by
case when convert(char(2),lTime,108)>=0 and convert(char(2),lTime,108)<=1
then '00:00-01:00'
when convert(char(2),lTime,108)>=1 and convert(char(2),lTime,108)<=2
then '01:00-02:00'
when convert(char(2),lTime,108)>=2 and convert(char(2),lTime,108)<=3
then '02:00-03:00'
when convert(char(2),lTime,108)>=3 and convert(char(2),lTime,108)<=4
then '03:00-04:00'
when convert(char(2),lTime,108)>=4 and convert(char(2),lTime,108)<=5
then '04:00-05:00'
when convert(char(2),lTime,108)>=5 and convert(char(2),lTime,108)<=6
then '05:00-06:00'
when convert(char(2),lTime,108)>=6 and convert(char(2),lTime,108)<=7
then '06:00-07:00'
when convert(char(2),lTime,108)>=7 and convert(char(2),lTime,108)<=8
then '07:00-08:00'
when convert(char(2),lTime,108)>=8 and convert(char(2),lTime,108)<=9
then '08:00-09:00'
when convert(char(2),lTime,108)>=9 and convert(char(2),lTime,108)<=10
then '09:00-10:00'
when convert(char(2),lTime,108)>=10 and convert(char(2),lTime,108)<=11
then '10:00-11:00'
when convert(char(2),lTime,108)>=11 and convert(char(2),lTime,108)<=12
then '11:00-12:00'
when convert(char(2),lTime,108)>=12 and convert(char(2),lTime,108)<=13
then '12:00-13:00'
when convert(char(2),lTime,108)>=13 and convert(char(2),lTime,108)<=14
then '13:00-14:00'
when convert(char(2),lTime,108)>=14 and convert(char(2),lTime,108)<=15
then '14:00-15:00'
when convert(char(2),lTime,108)>=15 and convert(char(2),lTime,108)<=16
then '15:00-16:00'
when convert(char(2),lTime,108)>=16 and convert(char(2),lTime,108)<=17
then '16:00-17:00'
when convert(char(2),lTime,108)>=17 and convert(char(2),lTime,108)<=18
then '17:00-18:00'
when convert(char(2),lTime,108)>=18 and convert(char(2),lTime,108)<=19
then '18:00-19:00'
when convert(char(2),lTime,108)>=19 and convert(char(2),lTime,108)<=20
then '19:00-20:00'
when convert(char(2),lTime,108)>=20 and convert(char(2),lTime,108)<=21
then '20:00-21:00'
when convert(char(2),lTime,108)>=21 and convert(char(2),lTime,108)<=22
then '21:00-22:00'
when convert(char(2),lTime,108)>=22 and convert(char(2),lTime,108)<=23
then '22:00-23:00'
when convert(char(2),lTime,108)>=23 and convert(char(2),lTime,108)<=24
then '23:00-00:00' end
--表是ListTable 时间字段lTime,数量字段lNum

⑻ SQL按时间分组查询

select MONTH(B),sum(A) FROM tt GROUP BY MONTH(B)

⑼ sql语句按照日期分组怎么写

1.如果你的时间格式是2012-01-13
17:35:52.217这样的形式,(主要是那个01不要是1),用下面这个
SELECT
convert(varchar(10),时间,23),SUM(
数字数据
)
FROM

group
by
convert(varchar(10),时间,23)
2.如果你的时间格式不是上面的格式,先转化成datetime:
SELECT
convert(varchar(10),cast(时间
as
datetime),23)
,SUM(数字数据)
FROM

group
by
convert(varchar(10),cast(时间
as
datetime),23)
3.如果报错,请追问~

⑽ 按日期分组的sql,我这里提示为什么缺少表达式啊

那个回答, 是针对 SQL Server 数据库的。

你的数据库, 会不会是 Oracle 啊?

如果是 Oracle 的话, 需要换一个函数。

SQL Server 里面的

Convert ( VARCHAR(10), 日期, 120)

在 Oracle 里面, 要修改为:
TO_CHAR( 日期, 'YYYY-MM-DD' )