我的表中有2个字段,NEXTTIME和ENDTIME,表名是VISIT。
我会从我的页面获取一个值00:05:00或类似的那样。最短是5分钟。
我将不得不根据该值从数据库进行查询。
我将获得最早的NEXTTIME,那个时间+ 00:05:00。
让我们说最早的NEXTTIME是06:44。
我的查询将像06:45,06:50,等等..
我还有另一个输入值,即间隔时间。
Nvm那个。
我只想知道如何按间隔查询。
结果看起来像这样。
NEXTTIME:ENDTIME
06:55:00~06:58:00
07:25:00~07:28:00
07:35:00~08:52:00
08:38:00~08:48:00
08:40:00~08:54:00
08:43:00~09:36:00
09:12:00~09:30
答案 0 :(得分:1)
这是基于所提供的信息以及我对您提出的解决方案的理解。为了满足图表周期,只需更改每个联合查询的select语句和where子句中的句点。
USE master
GO
create database Tests
GO
USE Tests
GO
create table Visits (
nexttime datetime not null
, endtime datetime not null
)
GO
BEGIN TRANSACTION
insert into Visits (nexttime, endtime) select N'06:55:00', N'06:58:00'
insert into Visits (nexttime, endtime) select N'07:25:00', N'07:28:00'
insert into Visits (nexttime, endtime) select N'07:35:00', N'08:52:00'
insert into Visits (nexttime, endtime) select N'08:38:00', N'08:48:00'
insert into Visits (nexttime, endtime) select N'08:40:00', N'08:54:00'
insert into Visits (nexttime, endtime) select N'08:43:00', N'09:36:00'
insert into Visits (nexttime, endtime) select N'09:12:00', N'09:30:00'
COMMIT
select N'06:30 - 07:00' as period
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 5 and 10 then 1 else 0 end) as [5:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 10 and 15 then 1 else 0 end) as [10:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 15 and 20 then 1 else 0 end) as [15:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 20 and 25 then 1 else 0 end) as [20:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 25 and 30 then 1 else 0 end) as [25:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 30 and 35 then 1 else 0 end) as [30:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 35 and 40 then 1 else 0 end) as [35:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 40 and 45 then 1 else 0 end) as [40:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 45 and 50 then 1 else 0 end) as [45:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) >= 50 then 1 else 0 end) as [50:00]
from Visits
where nexttime between CONVERT(datetime, N'06:30:00', 108) and CONVERT(datetime, N'07:00:00', 108)
union
select N'07:00 - 07:30' as period
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 5 and 10 then 1 else 0 end) as [5:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 10 and 15 then 1 else 0 end) as [10:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 15 and 20 then 1 else 0 end) as [15:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 20 and 25 then 1 else 0 end) as [20:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 25 and 30 then 1 else 0 end) as [25:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 30 and 35 then 1 else 0 end) as [30:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 35 and 40 then 1 else 0 end) as [35:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 40 and 45 then 1 else 0 end) as [40:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 45 and 50 then 1 else 0 end) as [45:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) >= 50 then 1 else 0 end) as [50:00]
from Visits
where nexttime between CONVERT(datetime, N'07:00:00', 108) and CONVERT(datetime, N'07:30:00', 108)
union
select N'07:30 - 08:00' as period
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 5 and 10 then 1 else 0 end) as [5:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 10 and 15 then 1 else 0 end) as [10:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 15 and 20 then 1 else 0 end) as [15:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 20 and 25 then 1 else 0 end) as [20:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 25 and 30 then 1 else 0 end) as [25:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 30 and 35 then 1 else 0 end) as [30:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 35 and 40 then 1 else 0 end) as [35:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 40 and 45 then 1 else 0 end) as [40:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 45 and 50 then 1 else 0 end) as [45:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) >= 50 then 1 else 0 end) as [50:00]
from Visits
where nexttime between CONVERT(datetime, N'07:30:00', 108) and CONVERT(datetime, N'08:00:00', 108)
union
select N'08:00 - 08:30' as period
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 5 and 10 then 1 else 0 end) as [5:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 10 and 15 then 1 else 0 end) as [10:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 15 and 20 then 1 else 0 end) as [15:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 20 and 25 then 1 else 0 end) as [20:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 25 and 30 then 1 else 0 end) as [25:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 30 and 35 then 1 else 0 end) as [30:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 35 and 40 then 1 else 0 end) as [35:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 40 and 45 then 1 else 0 end) as [40:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 45 and 50 then 1 else 0 end) as [45:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) >= 50 then 1 else 0 end) as [50:00]
from Visits
where nexttime between CONVERT(datetime, N'08:00:00', 108) and CONVERT(datetime, N'08:30:00', 108)
union
select N'08:30 - 09:00' as period
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 5 and 10 then 1 else 0 end) as [5:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 10 and 15 then 1 else 0 end) as [10:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 15 and 20 then 1 else 0 end) as [15:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 20 and 25 then 1 else 0 end) as [20:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 25 and 30 then 1 else 0 end) as [25:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 30 and 35 then 1 else 0 end) as [30:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 35 and 40 then 1 else 0 end) as [35:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 40 and 45 then 1 else 0 end) as [40:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 45 and 50 then 1 else 0 end) as [45:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) >= 50 then 1 else 0 end) as [50:00]
from Visits
where nexttime between CONVERT(datetime, N'08:30:00', 108) and CONVERT(datetime, N'09:00:00', 108)
union
select N'09:00 - 09:30' as period
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 5 and 10 then 1 else 0 end) as [5:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 10 and 15 then 1 else 0 end) as [10:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 15 and 20 then 1 else 0 end) as [15:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 20 and 25 then 1 else 0 end) as [20:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 25 and 30 then 1 else 0 end) as [25:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 30 and 35 then 1 else 0 end) as [30:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 35 and 40 then 1 else 0 end) as [35:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 40 and 45 then 1 else 0 end) as [40:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) between 45 and 50 then 1 else 0 end) as [45:00]
, SUM(case when DATEDIFF(MI, nexttime, endtime) >= 50 then 1 else 0 end) as [50:00]
from Visits
where nexttime between CONVERT(datetime, N'09:00:00', 108) and CONVERT(datetime, N'09:30:00', 108)
<强>输出强>
╔══════════════╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗
║06:30 - 07:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║
╠══════════════╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║07:00 - 07:30 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║
╠══════════════╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║07:30 - 08:00 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║
╠══════════════╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║08:00 - 08:30 ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║
╠══════════════╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║08:30 - 09:00 ║ 1 ║ 2 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║
╠══════════════╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣
║09:00 - 09:30 ║ 0 ║ 0 ║ 1 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║
╚══════════════╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝
随意说我是否正确理解了你的问题。