2010-12-21 118 views
1

我的表中有两个字段,NEXTTIME和ENDTIME,表名是VISIT。按期查询sql查询

我会从我的页面得到一个值,即00:05:00或者那样。最少5分钟。我将不得不根据该值从数据库中进行查询。

我会得到最早的NEXTTIME和那个时间+ 00:05:00。

比方说,最近的NEXTTIME是06:44。

我的查询会像06:45,06:50,等等..

我也会有另一个输入值是间隔时间。

Nvm that。

我只是想知道如何获得间隔查询。

结果会看起来像这样。 alt text

NEXTTIME:ENDTIME

6时55分00秒〜6点58分零零秒

7时25分00秒〜7点28分00秒

7时35分〇〇秒〜08 :52:00

八点38分00秒〜8时48分00秒

8点40分零零秒〜8时54分零零秒

8点43分00秒〜9时36分00秒

九时12分00秒〜09:30:00

+0

所以,你想半小时为行,五分钟间隔为列?而且对于每一列,你需要5分钟的访问次数,这是否正确? – 2010-12-21 03:43:35

+0

您能否提供示例数据,以便我们大家都可以针对我们的本地测试数据库测试一些查询? – 2010-12-21 03:45:05

+0

我已经编辑了带有示例数据的问题。 – william 2010-12-21 03:49:20

回答

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 ║ 
╚══════════════╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝ 

随意说我是否正确地理解您的问题或没有。

+0

这很棒..但是,正如我所说的。周期和间隔时间是可变的。就像用户将键入5分钟或15分钟。 – william 2010-12-21 06:35:36

+0

我们必须据此计算。 – william 2010-12-21 06:36:07

+0

'between'子句中的值是可以改变的。只需在参数中设置,即可完成。这是否符合要求? – 2010-12-21 07:00:48