获取 SQL 服务器中的日期时间计数范围
Getting datetime count range in SQL Server
我尝试获取 SQL 服务器中数据更改之间的日期范围
我的查询是
select count(1) as qty, Info, convert(char,dFError,100) dErr
from TableData
group by Info, convert(char,dFError,100)
order by dErr asc
我有这个
qty 有对服务器的请求数,信息是服务器 ip 和请求发送到另一台服务器的日期。
qty
Info
dErr
1
1.97
Aug 11 2021 9:01AM
1
1.97
Aug 11 2021 9:06AM
88
1.33
Dec 21 2021 2:04PM
1
1.95
Dec 22 2021 9:44PM
9
1.95
Dec 22 2021 9:45PM
1
1.33
Dec 22 2021 9:51PM
19
1.33
Dec 22 2021 9:52PM
3
1.33
Dec 22 2021 9:53PM
6
1.33
Dec 27 2021 7:10PM
17
1.33
Dec 27 2021 7:11PM
15
1.95
Dec 27 2021 7:17PM
8
1.95
Dec 27 2021 7:18PM
我想要这个,在 8 月 11 日 9:06AM 所有都将变为 1.97,在 12 月 21 日 2:04PM 所有都将变为 1.33,这意味着日期和信息
qty
Info
dErr
2
1.97
Aug 11 2021 9:06AM
88
1.33
Dec 21 2021 2:04PM
10
1.95
Dec 22 2021 9:45PM
46
1.33
Dec 27 2021 7:11PM
23
1.95
Dec 27 2021 7:18PM
同一天可以是不同时间的同一组号码
qty
Info
dErr
1
1.97
Jan 24 2022 9:39AM
1
1.97
Jan 24 2022 9:51AM
1
1.97
Jan 24 2022 9:58AM
4
1.97
Jan 24 2022 10:08AM
1
1.97
Jan 24 2022 10:12AM
8
1.95
Jan 24 2022 10:24AM
2
1.95
Jan 24 2022 10:32AM
10
1.33
Jan 24 2022 10:33AM
1
1.33
Jan 24 2022 11:37AM
8
1.95
Jan 24 2022 11:59AM
1
1.95
Jan 24 2022 12:00PM
2
1.95
Jan 24 2022 12:08PM
需要像
一样显示
qty
Info
dErr
8
1.97
Jan 24 2022 10:12AM
10
1.95
Jan 24 2022 10:32AM
11
1.33
Jan 24 2022 11:37AM
11
1.95
Jan 24 2022 12:08PM
select
SUM(P_COUNT) as "COUNT",
P_DATA as "DATA",
MAX(FECHA) as "FECHA"
from
TABLEA
GROUP BY
P_DATA, CONVERT(DATE, FECHA)
ORDER BY "FECHA"
您的预期结果与给定数据不匹配 - 在第一组中,您有 12/22 的行,同时包含 1.33 和 1.95,但未包含在您的预期结果中。
在我看来,您想按日期分组 - 或 date\hour。这是两者的示例:
Declare @testTable table (qty int, Info numeric(3,2), dErr datetime);
Insert Into @testTable (qty, Info, dErr)
Values ( 1, 1.97, 'Aug 11 2021 9:01AM')
, ( 1, 1.97, 'Aug 11 2021 9:06AM')
, (88, 1.33, 'Dec 21 2021 2:04PM')
, ( 1, 1.95, 'Dec 22 2021 9:44PM')
, ( 9, 1.95, 'Dec 22 2021 9:45PM')
, ( 1, 1.33, 'Dec 22 2021 9:51PM')
, (19, 1.33, 'Dec 22 2021 9:52PM')
, ( 3, 1.33, 'Dec 22 2021 9:53PM')
, ( 6, 1.33, 'Dec 27 2021 7:10PM')
, (17, 1.33, 'Dec 27 2021 7:11PM')
, (15, 1.95, 'Dec 27 2021 7:17PM')
, ( 8, 1.95, 'Dec 27 2021 7:18PM')
, ( 1, 1.97, 'Jan 24 2022 9:39AM')
, ( 1, 1.97, 'Jan 24 2022 9:51AM')
, ( 1, 1.97, 'Jan 24 2022 9:58AM')
, ( 4, 1.97, 'Jan 24 2022 10:08AM')
, ( 1, 1.97, 'Jan 24 2022 10:12AM')
, ( 8, 1.95, 'Jan 24 2022 10:24AM')
, ( 2, 1.95, 'Jan 24 2022 10:32AM')
, (10, 1.33, 'Jan 24 2022 10:33AM')
, ( 1, 1.33, 'Jan 24 2022 11:37AM')
, ( 8, 1.95, 'Jan 24 2022 11:59AM')
, ( 1, 1.95, 'Jan 24 2022 12:00PM')
, ( 2, 1.95, 'Jan 24 2022 12:08PM');
--==== Grouped by date
Select total_qty = sum(tt.qty)
, tt.Info
, latest_date = max(tt.dErr)
From @testTable tt
Group By
tt.Info
, cast(tt.dErr As date)
Order By
cast(tt.dErr As date);
--==== Grouped by date\hour
Select total_qty = sum(tt.qty)
, tt.Info
, latest_date = max(tt.dErr)
From @testTable tt
Group By
tt.Info
, cast(tt.dErr As date)
, datepart(Hour, tt.dErr)
Order By
cast(tt.dErr As date)
, datepart(Hour, tt.dErr);
双row_number可用于计算排名。
然后可以在聚合中使用排名来解决这种Gaps-And-Islands类型的问题。
select sum(qty) as qty, Info, max(dFError) as dErr
from (
select Info, dFError, qty
, convert(date, dFError) as dErrorDate
, Rnk = row_number() over (order by dFError)
+ row_number() over (partition by Info order by dFError desc)
from TableData
) q
group by Info, Rnk
order by dErr;
qty
Info
dErr
2
1.97
2021-08-11 09:06:00.000
88
1.33
2021-12-21 14:04:00.000
10
1.95
2021-12-22 21:45:00.000
46
1.33
2021-12-27 19:11:00.000
23
1.95
2021-12-27 19:18:00.000
8
1.97
2022-01-24 10:12:00.000
10
1.95
2022-01-24 10:32:00.000
11
1.33
2022-01-24 11:37:00.000
11
1.95
2022-01-24 12:08:00.000
演示 db<>fiddle here
我尝试获取 SQL 服务器中数据更改之间的日期范围 我的查询是
select count(1) as qty, Info, convert(char,dFError,100) dErr
from TableData
group by Info, convert(char,dFError,100)
order by dErr asc
我有这个 qty 有对服务器的请求数,信息是服务器 ip 和请求发送到另一台服务器的日期。
qty | Info | dErr |
---|---|---|
1 | 1.97 | Aug 11 2021 9:01AM |
1 | 1.97 | Aug 11 2021 9:06AM |
88 | 1.33 | Dec 21 2021 2:04PM |
1 | 1.95 | Dec 22 2021 9:44PM |
9 | 1.95 | Dec 22 2021 9:45PM |
1 | 1.33 | Dec 22 2021 9:51PM |
19 | 1.33 | Dec 22 2021 9:52PM |
3 | 1.33 | Dec 22 2021 9:53PM |
6 | 1.33 | Dec 27 2021 7:10PM |
17 | 1.33 | Dec 27 2021 7:11PM |
15 | 1.95 | Dec 27 2021 7:17PM |
8 | 1.95 | Dec 27 2021 7:18PM |
我想要这个,在 8 月 11 日 9:06AM 所有都将变为 1.97,在 12 月 21 日 2:04PM 所有都将变为 1.33,这意味着日期和信息
qty | Info | dErr |
---|---|---|
2 | 1.97 | Aug 11 2021 9:06AM |
88 | 1.33 | Dec 21 2021 2:04PM |
10 | 1.95 | Dec 22 2021 9:45PM |
46 | 1.33 | Dec 27 2021 7:11PM |
23 | 1.95 | Dec 27 2021 7:18PM |
同一天可以是不同时间的同一组号码
qty | Info | dErr |
---|---|---|
1 | 1.97 | Jan 24 2022 9:39AM |
1 | 1.97 | Jan 24 2022 9:51AM |
1 | 1.97 | Jan 24 2022 9:58AM |
4 | 1.97 | Jan 24 2022 10:08AM |
1 | 1.97 | Jan 24 2022 10:12AM |
8 | 1.95 | Jan 24 2022 10:24AM |
2 | 1.95 | Jan 24 2022 10:32AM |
10 | 1.33 | Jan 24 2022 10:33AM |
1 | 1.33 | Jan 24 2022 11:37AM |
8 | 1.95 | Jan 24 2022 11:59AM |
1 | 1.95 | Jan 24 2022 12:00PM |
2 | 1.95 | Jan 24 2022 12:08PM |
需要像
一样显示qty | Info | dErr |
---|---|---|
8 | 1.97 | Jan 24 2022 10:12AM |
10 | 1.95 | Jan 24 2022 10:32AM |
11 | 1.33 | Jan 24 2022 11:37AM |
11 | 1.95 | Jan 24 2022 12:08PM |
select
SUM(P_COUNT) as "COUNT",
P_DATA as "DATA",
MAX(FECHA) as "FECHA"
from
TABLEA
GROUP BY
P_DATA, CONVERT(DATE, FECHA)
ORDER BY "FECHA"
您的预期结果与给定数据不匹配 - 在第一组中,您有 12/22 的行,同时包含 1.33 和 1.95,但未包含在您的预期结果中。
在我看来,您想按日期分组 - 或 date\hour。这是两者的示例:
Declare @testTable table (qty int, Info numeric(3,2), dErr datetime);
Insert Into @testTable (qty, Info, dErr)
Values ( 1, 1.97, 'Aug 11 2021 9:01AM')
, ( 1, 1.97, 'Aug 11 2021 9:06AM')
, (88, 1.33, 'Dec 21 2021 2:04PM')
, ( 1, 1.95, 'Dec 22 2021 9:44PM')
, ( 9, 1.95, 'Dec 22 2021 9:45PM')
, ( 1, 1.33, 'Dec 22 2021 9:51PM')
, (19, 1.33, 'Dec 22 2021 9:52PM')
, ( 3, 1.33, 'Dec 22 2021 9:53PM')
, ( 6, 1.33, 'Dec 27 2021 7:10PM')
, (17, 1.33, 'Dec 27 2021 7:11PM')
, (15, 1.95, 'Dec 27 2021 7:17PM')
, ( 8, 1.95, 'Dec 27 2021 7:18PM')
, ( 1, 1.97, 'Jan 24 2022 9:39AM')
, ( 1, 1.97, 'Jan 24 2022 9:51AM')
, ( 1, 1.97, 'Jan 24 2022 9:58AM')
, ( 4, 1.97, 'Jan 24 2022 10:08AM')
, ( 1, 1.97, 'Jan 24 2022 10:12AM')
, ( 8, 1.95, 'Jan 24 2022 10:24AM')
, ( 2, 1.95, 'Jan 24 2022 10:32AM')
, (10, 1.33, 'Jan 24 2022 10:33AM')
, ( 1, 1.33, 'Jan 24 2022 11:37AM')
, ( 8, 1.95, 'Jan 24 2022 11:59AM')
, ( 1, 1.95, 'Jan 24 2022 12:00PM')
, ( 2, 1.95, 'Jan 24 2022 12:08PM');
--==== Grouped by date
Select total_qty = sum(tt.qty)
, tt.Info
, latest_date = max(tt.dErr)
From @testTable tt
Group By
tt.Info
, cast(tt.dErr As date)
Order By
cast(tt.dErr As date);
--==== Grouped by date\hour
Select total_qty = sum(tt.qty)
, tt.Info
, latest_date = max(tt.dErr)
From @testTable tt
Group By
tt.Info
, cast(tt.dErr As date)
, datepart(Hour, tt.dErr)
Order By
cast(tt.dErr As date)
, datepart(Hour, tt.dErr);
双row_number可用于计算排名。
然后可以在聚合中使用排名来解决这种Gaps-And-Islands类型的问题。
select sum(qty) as qty, Info, max(dFError) as dErr from ( select Info, dFError, qty , convert(date, dFError) as dErrorDate , Rnk = row_number() over (order by dFError) + row_number() over (partition by Info order by dFError desc) from TableData ) q group by Info, Rnk order by dErr;
qty Info dErr 2 1.97 2021-08-11 09:06:00.000 88 1.33 2021-12-21 14:04:00.000 10 1.95 2021-12-22 21:45:00.000 46 1.33 2021-12-27 19:11:00.000 23 1.95 2021-12-27 19:18:00.000 8 1.97 2022-01-24 10:12:00.000 10 1.95 2022-01-24 10:32:00.000 11 1.33 2022-01-24 11:37:00.000 11 1.95 2022-01-24 12:08:00.000
演示 db<>fiddle here