Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么这个tsql查询没有';你不能选择它想要什么?_Sql_Sql Server 2005_Tsql - Fatal编程技术网

为什么这个tsql查询没有';你不能选择它想要什么?

为什么这个tsql查询没有';你不能选择它想要什么?,sql,sql-server-2005,tsql,Sql,Sql Server 2005,Tsql,我想获得某些客户的小时统计数据,如果没有值,我还是要选择0 此查询仅选择统计表中存在的行,即使我已使用Calendar CTE离开联接 谢谢,这是因为您在where子句中引用了统计表 尝试: 这是因为您在where子句中引用了统计表 尝试: declare @customerID int set @customerID=1 ;with cteDates as ( select dateadd(hh, datediff(hh, 0, getdate()), 0) as [Date] u

我想获得某些客户的小时统计数据,如果没有值,我还是要选择0

此查询仅选择统计表中存在的行,即使我已使用Calendar CTE离开联接


谢谢,

这是因为您在where子句中引用了统计表

尝试:


这是因为您在where子句中引用了统计表

尝试:

declare @customerID int
set @customerID=1


;with cteDates as
(
  select dateadd(hh, datediff(hh, 0, getdate()), 0) as [Date]
  union all
  select dateadd(hh, -1, [Date]) as [Date]
  from cteDates
  where [Date] > dateadd(hh, -23, getdate())
)
select
    sum(coalesce(field1,0)) field1,
    sum(coalesce(field2,0)) field2,
    sum(coalesce(field3,0)) field3,
    sum(coalesce(field4,0)) field4,
    sum(coalesce(field5,0)) field5,
    d.[Date]
from cteDates as d left join [Statistics] s
on d.Date=s.date
where s.customerID=@customerID and s.date>dateadd(hh,-24,getdate())
group by d.Date
declare @customerID int
set @customerID=1

;with cteDates as
(
select dateadd(hh, datediff(hh, 0, getdate()), 0) as [Date]
union all
select dateadd(hh, -1, [Date]) as [Date]
from cteDates
where [Date] > dateadd(hh, -23, getdate())
)
select
sum(coalesce(field1,0)) field1,
sum(coalesce(field2,0)) field2,
sum(coalesce(field3,0)) field3,
sum(coalesce(field4,0)) field4,
sum(coalesce(field5,0)) field5,
d.[Date]
from cteDates as d left join [Statistics] s
on d.Date=s.date
and s.customerID=@customerID and s.date>dateadd(hh,-24,getdate())
group by d.Date