1

我在 SQL Server 中有一个表,其中包含一些数据和一datetime列。

我想在 20:00 到 05:00 时钟之间获取数据。

我有以下查询,但无法成功

select 
    txt_target_cell_id, count(*) as 'Total Count', txt_longitude, txt_latitude
from 
    tbl_cdr_analyzer_load
where 
    DATEPART(hour, dat_start) between 20 and 05
group by 
    txt_target_cell_id, txt_longitude, txt_latitude
4

2 回答 2

4

你的BETWEEN说法意味着:

DATEPART(hour, dat_start) >= 20 AND DATEPART(hour, dat_start)<=04

这总是错误的。

您可以尝试这样做:

select 
    txt_target_cell_id,count(*) as 'Total Count',txt_longitude,txt_latitude
from 
    tbl_cdr_analyzer_load
where 
    DATEPART(hour, dat_start) <= 04 OR DATEPART(hour, dat_start)>=20
group by 
    txt_target_cell_id,txt_longitude,txt_latitude
于 2013-05-01T05:17:25.167 回答
1
select 
    txt_target_cell_id, count(*) as 'Total Count', txt_longitude, txt_latitude
from 
    tbl_cdr_analyzer_load
where 
    DATEPART(hour, dat_start) between 04 and 20 //Change here
group by 
    txt_target_cell_id, txt_longitude, txt_latitude

It because the id Between valu1 and value2 is equivalent to. where id>=value1 and id<=values2 to get desire result,,,,

Select 
    txt_target_cell_id,count(*) as 'Total Count',txt_longitude,txt_latitude
from 
    tbl_cdr_analyzer_load
where 
    DATEPART(hour, dat_start) <= 04 OR DATEPART(hour, dat_start)>=20
group by 
    txt_target_cell_id,txt_longitude,txt_latitude
于 2013-05-01T05:19:54.610 回答