表字段有:站号、日期、气温
主键为站点和日期 现在查询2008年8月1号到3号气温总和、2009年8月1号到3号气温总和、以及两个的差值。
我是这么写的:
select s2.站号,sum(s1.气温), sum(s2.气温),sum(s2.气温)-sum(s1.气温) from 气象表 s1,气象表 s2
where s1.站号=s2.站号
and s2.日期>=to_date('2009-8-1','yyyy-mm-dd') and s2.日期 <=to_date('2009-8-3','yyyy-mm-dd')
and s1.日期>=to_date('2008-8-1','yyyy-mm-dd') and s1.日期 <=to_date('2008-8-3','yyyy-mm-dd')
group by s2.站号; 这样查出来的结果总是实际值的3倍,我想是因为我累计了3天。我想还是关联条件的问题,因为主键是站号和日期,而关联条件只有站号。
但是我又不知道怎么写,麻烦各位帮着看看。谢谢。
主键为站点和日期 现在查询2008年8月1号到3号气温总和、2009年8月1号到3号气温总和、以及两个的差值。
我是这么写的:
select s2.站号,sum(s1.气温), sum(s2.气温),sum(s2.气温)-sum(s1.气温) from 气象表 s1,气象表 s2
where s1.站号=s2.站号
and s2.日期>=to_date('2009-8-1','yyyy-mm-dd') and s2.日期 <=to_date('2009-8-3','yyyy-mm-dd')
and s1.日期>=to_date('2008-8-1','yyyy-mm-dd') and s1.日期 <=to_date('2008-8-3','yyyy-mm-dd')
group by s2.站号; 这样查出来的结果总是实际值的3倍,我想是因为我累计了3天。我想还是关联条件的问题,因为主键是站号和日期,而关联条件只有站号。
但是我又不知道怎么写,麻烦各位帮着看看。谢谢。
from
(select 站号,sum(气温) 气温 from s1 where to_char(日期,'YYYY-MM-DD') between '2008-08-01' and '2008-08-03' group by 站号)A,
(select 站号,sum(气温) 气温 from s2 where to_char(日期,'YYYY-MM-DD') between '2008-09-01' and '2009-08-03' group by 站号)B,
where A.站号=B.站号
from
(select 站号,sum(气温) 气温 from s1 where to_char(日期,'YYYY-MM-DD') between '2008-08-01' and '2008-08-03' group by 站号)A,
(select 站号,sum(气温) 气温 from s2 where to_char(日期,'YYYY-MM-DD') between '2009-08-01' and '2009-08-03' group by 站号)B,
where A.站号=B.站号
请问“A”和“B”能这样直接写吗?
可以select A.站号,A.气温 气温A,B.气温 气温B,A.气温-B.气温 气温差
from
(select 站号,sum(气温) 气温 from 气象表 s1 where to_char(日期,'YYYY-MM-DD') between '2008-08-01' and '2008-08-03' group by 站号)A,
(select 站号,sum(气温) 气温 from 气象表 s2 where to_char(日期,'YYYY-MM-DD') between '2009-08-01' and '2009-08-03' group by 站号)B
where A.站号=B.站号 去掉了 B 后面的","
select A.站号,A.气温 气温A,B.气温 气温B,A.气温-B.气温 气温差
from
(select 站号,sum(气温) 气温 from 气象表 s1 where to_char(日期,'YYYY-MM-DD') between '2008-08-01' and '2008-08-03' group by 站号) A,
(select 站号,sum(气温) 气温 from 气象表 s2 where to_char(日期,'YYYY-MM-DD') between '2009-08-01' and '2009-08-03' group by 站号) B
where A.站号=B.站号 A和前面的(select ...)添个空格(B也一样):(select ...) A, (select....) B where.....
from
(select IIIII,sum(V12004) 气温 from T_mete_history where to_char(vdate,'YYYY-MM-DD') between '2008-08-01' and '2008-08-03' group by IIIII)A,
(select IIIII,sum(V12004) 气温 from T_mete_history where to_char(vdate,'YYYY-MM-DD') between '2009-08-01' and '2009-08-03' group by IIIII)B
where A.IIIII=B.IIIII;
但是说第一行有错,无效的标识符,请问怎么回事呀?