日期:2014-05-18  浏览次数:20558 次

请教一个sql查询的问题
我有两个数据表,一个是小时数据表,一个是分钟数据表,我的要求是在小时数据表中查询每个小时的数据(查3个小时的),然后在分钟数据表中查询当前这个小时的分钟数据,最后把这两个数据求和
我的查询语句如下:
select stationid,sum(tt) from
(
select stationid, tt from tabhourdata
  where stationid='R1234'  
  and observtime between '2012-3-21 08:00:00' and '2012-3-21 10:00:00'
union all
 select stationid, tt from tabminutedata 
  where stationid='R1234'  
  and observtime between '2012-3-21 10:00:00' and '2012-3-21 11:00:00'
  and observtime <> '2012-3-21 10:00:00'
)
group by stationid

数据库使用的是sql2005,运行以上查询语句,提示
消息 156,级别 15,状态 1,第 12 行
在关键字 'group' 附近有语法错误

请教这个是什么问题,另外,如果不查询求和,就是一下查询语句的时候:

select stationid, tt from tabhourdata
  where stationid='R1234'  
  and observtime between '2012-3-21 08:00:00' and '2012-3-21 10:00:00'
union all
 select stationid, tt from tabminutedata 
  where stationid='R1234'  
  and observtime between '2012-3-21 10:00:00' and '2012-3-21 11:00:00'
  and observtime <> '2012-3-21 10:00:00'

是可以查询出数据的,现在就是添加求和查询语句后就出问题

------解决方案--------------------
select stationid,sum(tt) from
(
select stationid, tt from tabhourdata
where stationid='R1234'
and observtime between '2012-3-21 08:00:00' and '2012-3-21 10:00:00'
union all
 select stationid, tt from tabminutedata
where stationid='R1234'
and observtime between '2012-3-21 10:00:00' and '2012-3-21 11:00:00'
and observtime <> '2012-3-21 10:00:00'
) AS T
group by stationid


语法问题,看红色