我在SQL 2005 DB中有两列数据
Datetime Value
'2009-10-29 10:00:00' ' 10.1'
'2009-10-29 10:15:00' ' 10.2'
'2009-10-29 10:30:00' ' 10.3'
'2009-10-29 10:45:00' ' 10.4'
我想
SELECT Value
FROM [table]
WHERE Datetime >= '2009-10-29 10:00:00'
AND (Datetime NOT BETWEEN '2009-10-29 10:14:00' AND '2009-10-29 10:16:00' )
AND Datetime < '2009-10-29 10:35:00'
我原本希望得到
10.1
10.3
但事实并非如此......帮助?
好的,这是我正在使用的实际sql语句;
Declare @Tagname varchar(25)
Set @Tagname = '52_RTU_#RE1002'
SELECT DateTIme,(Runtime.dbo.v_AnalogHistory.Value), @Tagname As Tagname,0
FROM Runtime.dbo.AnalogTag INNER JOIN Runtime.dbo.v_AnalogHistory ON Runtime.dbo.AnalogTag.TagName = Runtime.dbo.v_AnalogHistory.TagName
WHERE(Runtime.dbo.v_AnalogHistory.TagName IN (@Tagname))
AND(Runtime.dbo.v_AnalogHistory.wwVersion = 'Latest')
AND(Runtime.dbo.v_AnalogHistory.wwRetrievalMode = 'Full')
AND Datetime >= '2009-08-01 00:00:00'
AND (Datetime NOT BETWEEN '2009-08-01 10:27:00' AND '2009-08-01 11:30:00')
AND Datetime < '2009-08-01 11:35:00'
这是没有应用NOT BETWEEN语句的数据集:
2009-08-01 00:00:00.000 0.72 52_RTU_#RE1002
2009-08-01 10:25:15.300 0.44 52_RTU_#RE1002
2009-08-01 10:27:22.350 0.5 52_RTU_#RE1002
2009-08-01 10:27:25.350 0.56 52_RTU_#RE1002
2009-08-01 10:27:27.360 0.62 52_RTU_#RE1002
2009-08-01 10:27:28.760 0.68 52_RTU_#RE1002
2009-08-01 10:27:30.360 0.74 52_RTU_#RE1002
2009-08-01 10:27:31.560 0.8 52_RTU_#RE1002
2009-08-01 10:27:32.760 0.87 52_RTU_#RE1002
2009-08-01 10:27:33.960 0.94 52_RTU_#RE1002
2009-08-01 10:27:35.370 1 52_RTU_#RE1002
2009-08-01 10:27:36.360 1.05999992370605 52_RTU_#RE1002
2009-08-01 10:27:37.570 1.13 52_RTU_#RE1002
2009-08-01 10:27:38.760 1.19000007629395 52_RTU_#RE1002
2009-08-01 10:27:40.360 1.25 52_RTU_#RE1002
2009-08-01 10:27:41.760 1.31 52_RTU_#RE1002
2009-08-01 10:27:43.560 1.37 52_RTU_#RE1002
2009-08-01 10:27:46.360 1.43 52_RTU_#RE1002
2009-08-01 10:27:57.580 1.37 52_RTU_#RE1002
2009-08-01 10:28:00.380 1.31 52_RTU_#RE1002
2009-08-01 10:28:02.580 1.25 52_RTU_#RE1002
2009-08-01 10:28:04.980 1.19000007629395 52_RTU_#RE1002
2009-08-01 10:28:07.390 1.13 52_RTU_#RE1002
2009-08-01 10:28:09.590 1.07000007629395 52_RTU_#RE1002
2009-08-01 10:28:11.810 1.01 52_RTU_#RE1002
2009-08-01 10:28:14.480 0.95 52_RTU_#RE1002
2009-08-01 10:28:16.630 0.89 52_RTU_#RE1002
2009-08-01 10:28:19.430 0.83 52_RTU_#RE1002
2009-08-01 10:28:21.830 0.77 52_RTU_#RE1002
2009-08-01 10:28:24.850 0.71 52_RTU_#RE1002
2009-08-01 10:28:28.240 0.65 52_RTU_#RE1002
2009-08-01 10:28:31.450 0.589999961853027 52_RTU_#RE1002
2009-08-01 10:28:35.250 0.529999961853027 52_RTU_#RE1002
2009-08-01 10:28:39.460 0.47 52_RTU_#RE1002
2009-08-01 10:28:44.470 0.41 52_RTU_#RE1002
2009-08-01 10:28:49.860 0.35 52_RTU_#RE1002
2009-08-01 10:28:56.870 0.29 52_RTU_#RE1002
2009-08-01 10:29:05.880 0.23 52_RTU_#RE1002
2009-08-01 10:29:17.890 0.17 52_RTU_#RE1002
2009-08-01 10:29:32.320 0.11 52_RTU_#RE1002
2009-08-01 10:29:57.930 0.05 52_RTU_#RE1002
2009-08-01 10:32:46.360 0.01 52_RTU_#RE1002
2009-08-01 10:37:46.360 0.01 52_RTU_#RE1002
2009-08-01 10:42:46.260 0.01 52_RTU_#RE1002
2009-08-01 10:47:46.310 0.01 52_RTU_#RE1002
2009-08-01 10:52:46.360 0.01 52_RTU_#RE1002
2009-08-01 10:57:46.230 0.01 52_RTU_#RE1002
2009-08-01 11:02:46.300 0.01 52_RTU_#RE1002
2009-08-01 11:07:47.230 0.01 52_RTU_#RE1002
2009-08-01 11:12:47.370 0.03 52_RTU_#RE1002
2009-08-01 11:17:47.260 0.04 52_RTU_#RE1002
2009-08-01 11:22:12.490 0.11 52_RTU_#RE1002
2009-08-01 11:22:47.300 0.14 52_RTU_#RE1002
2009-08-01 11:27:47.360 0.14 52_RTU_#RE1002
2009-08-01 11:28:40.610 0.17 52_RTU_#RE1002
2009-08-01 11:32:48.330 0.13 52_RTU_#RE1002
2009-08-01 11:34:34.680 0.23 52_RTU_#RE1002
以下是未经过BETWEEN应用的数据
2009-08-01 11:32:48.330 0.13 52_RTU_#RE1002 0
2009-08-01 11:34:34.680 0.23 52_RTU_#RE1002 0
答案 0 :(得分:1)
使用:
SELECT t.value
FROM TABLE t
WHERE t.datetime BETWEEN '2009-10-29 10:00:00' AND '2009-10-29 10:13:59'
UNION ALL
SELECT t.value
FROM TABLE t
WHERE t.datetime BETWEEN '2009-10-29 10:16:01' AND '2009-10-29 10:34:59'
...现在尝试:
SELECT t.value
FROM TABLE t
WHERE (t.datetime BETWEEN '2009-10-29 10:00:00' AND '2009-10-29 10:13:59'
OR t.datetime BETWEEN '2009-10-29 10:16:01' AND '2009-10-29 10:34:59')
答案 1 :(得分:0)
您可以尝试这样的事情:
SELECT Value
FROM [table]
WHERE (Datetime >= '2009-10-29 10:00:00' AND Datetime < '2009-10-29 10:14:00')
OR (Datetime > '2009-10-29 10:16:00' AND Datetime < '2009-10-29 10:35:00')
答案 2 :(得分:0)
那些实际的日期时间列是某种形式的varchar吗?我问,因为DB2中的类似查询工作正常:
drop table xx; commit;
create table xx (val1 integer, val2 integer); commit;
insert into xx values (1000,101);
insert into xx values (1015,102);
insert into xx values (1030,103);
insert into xx values (1045,104);
commit;
select * from xx
where val1 >= 1000
and (val1 not between 1014 and 1029)
and val1 < 1035;
返回:
VAL1 VAL2
1000 101
1030 103
DSNE610I NUMBER OF ROWS DISPLAYED IS 2
事实上,即使有时间戳,我也能得到正确的结果:
drop table drlan.xx;
commit;
create table drlan.xx (
val1 timestamp,
val2 varchar(4)
);
commit;
insert into drlan.xx values ('2009-10-29 10:00:00','10.1');
insert into drlan.xx values ('2009-10-29 10:15:00','10.2');
insert into drlan.xx values ('2009-10-29 10:30:00','10.3');
insert into drlan.xx values ('2009-10-29 10:45:00','10.4');
commit;
select * from drlan.xx
where val1 >= '2009-10-29 10:00:00'
and (val1 not between '2009-10-29 10:14:00'
and '2009-10-29 10:16:00')
and val1 < '2009-10-29 10:35:00';
VAL1 VAL2
2009-10-29-10.00.00.000000 10.1
2009-10-29-10.30.00.000000 10.3
DSNE610I NUMBER OF ROWS DISPLAYED IS 2
这让我相信SQL Server存在某种问题。
答案 3 :(得分:0)
以下适用于SQL Server 2005。
create table xx (val1 datetime, val2 varchar(10));
insert into xx values ('2009-10-29 10:00:00', ' 10.1');
insert into xx values ('2009-10-29 10:15:00', ' 10.2');
insert into xx values ('2009-10-29 10:30:00', ' 10.3');
insert into xx values ('2009-10-29 10:45:00', ' 10.4');
SELECT val2
FROM [xx]
WHERE val1 >= '2009-10-29 10:00:00'
AND (val1 NOT BETWEEN '2009-10-29 10:14:00' AND '2009-10-29 10:16:00' )
AND val1 < '2009-10-29 10:35:00'
但如果你执行
insert into xx values ('2009-10-29 9:59:59.999', ' 10.8');
然后在SQL Server Management Studio中显示为
2009-10-29 10:00:00.000 10.8
您确定未接受的值吗?
答案 4 :(得分:0)
好的,因为事实证明存储/检索引擎(一种名为INsql的产品)sql从中获取数据似乎不适用于在statment事物中的多次。如果我选择整个数据集到一个临时表并使用sql在那里工作,NOT BETWEEN语句工作正常。谢谢大家的帮助,缩小范围!