SQL查询问题没有数据显示

时间:2014-01-17 13:35:46

标签: sql-server tsql

这是我的结果集,但是当我对结果集进行查询时,没有数据即将到来,但我无法理解查询中的错误。

call Start         Caller       direction Is_Internal continuation call duration       party1name
------------------ ------------ --------- ----------- ------------ ------------------ -----------------
1/15/2014 8:47     346241552    I         0           0            0:00:18             VM Kanaal 1
1/15/2014 9:56     252621028    I         0           0            0:00:17             Kanaal 1
1/15/2014 9:58     252621028    I         0           0            0:00:17             Kanaal 1
1/15/2014 9:01     252621028    I         0           1            0:00:08             Kanaal 1
1/15/2014 9:01     252621028    I         0           0            0:01:57             Coen
1/15/2014 9:06     302          O         0           0            0:01:53             Coen
1/15/2014 9:07     306          O         0           0            0:01:33             koos de Bruijn
1/15/2014 9:11     644686793    I         0           0            0:00:08             VM Kanaal 1
1/15/2014 9:11     644686793    I         0           0            0:01:46             Coen
1/15/2014 9:27     306          O         0           0            0:00:43             koos de Bruijn
1/15/2014 9:25     302          O         0           0            0:06:46             Coen
1/15/2014 9:46     455426194    I         0           1            0:00:07             VM Kanaal 1
1/15/2014 9:46     455426194    I         0           0            0:00:50             Coen
1/15/2014 9:57     725716251    I         0           1            0:00:10             VM Kanaal 1
1/15/2014 10:00    0            I         O           1            0:00:00             Voicemail

我的查询在这里

SELECT Convert(varchar,[call Start],101) Date,[Caller] [Phone No], Count(*) [Total Incomming calls]  FROM tridip
 where direction='I' and 
CAST([call Start] AS datetime) >= CONVERT(datetime,'09:00:00') and 
CAST([call Start] AS datetime) <= CONVERT(datetime,'17:30:00') 
AND Is_Internal=0 and continuation=0 AND
CONVERT(datetime,CAST([call duration] AS DATETIME),108) <> CAST('00:00:00' AS DATETIME) 
and  party1name not in ('Voice Mail') and party1name not like 'VM %' 
and party1name not like 'Line%'  
group by [Caller],Convert(varchar,[call Start],101)

告诉我我的查询有什么问题。它应该显示日期和电话号码和计数可能是1或大于1.请指导我改变什么。

在第二行有电话号码252621028,这是重复的,它的计数应该大于1.

感谢

更新

我想出了问题并纠正了sql如下

SELECT [Caller] [Phone No], Count(*) [Total Incomming calls]  FROM tridip
 where direction='I' and 
CONVERT(VARCHAR,[call Start],108) >= '09:00:00' and 
CONVERT(VARCHAR,[call Start],108) <= '17:30:00' 
AND Is_Internal=0 and continuation=0 AND
[call duration] <> '00:00:00'
and  party1name not in ('Voice Mail') and party1name not like 'VM %' 
and party1name not like 'Line%' and [Caller]>0
group by [Caller]

2 个答案:

答案 0 :(得分:5)

由于这种情况,你很可能没有得到结果 -

CAST([call Start] AS datetime) <= CONVERT(datetime,'17:30:00')

原因 -

试试这个 -

SELECT CONVERT(datetime,'17:30:00')

结果是 - 1900-01-01 17:30:00.000

我相信你的记录中没有一个[call start]时间少于此,因此没有结果。

你是否试图获得2次之间的所有记录?

答案 1 :(得分:0)

如前所述,您获得的值为1900-01-01 09:00的DateTime明显低于您的任何日期。

您还可以使用SQL Server的DATEPART功能更轻松地比较日期和时间:

where     direction='I'
and       DATEPART(hour, CAST([call Start] AS datetime)) >= 9
and       DATEPART(hour, CAST([call Start] AS datetime)) <= 17

(你需要稍微修改一下才能在17:30之前工作,但它会给你一般的想法。)