急:计算列的问题
两张表:
table1: 序号,日期, 数据 table2: 序号,属性
1 200401 10 1 ...
2 200402 20 2 ...
. . . . ...
在给定序号和日期的情况下,我想要得到这样的Sql:
select table2.序号,table2.属性, tbale1.日期,数据为200402和200401的差值(也就是20-10)
where 日期='200402' and table1.序号=table2.序号
怎样得到上面差值?这个sql怎么写?
问题点数:20、回复次数:9Top
1 楼netcoder(朱二)回复于 2004-11-04 12:53:55 得分 0
楼主把最终想要的记录集贴出来,别人也许就能明白Top
2 楼lovvver(ElephantTalk.Bright)回复于 2004-11-04 13:04:39 得分 0
select b.序号,b.属性, a.日期,a.数据-(select 数据 from table1,a where 序号=a.序号-1)
from table1 a,table2 b
where a.日期='200402' and a.序号=b.序号Top
3 楼lsxaa(小李铅笔刀)回复于 2004-11-04 13:05:12 得分 0
select b.序号,
b.属性,
a.日期,
(a.数据-(select 数据 from table1
where left(日期,4)=left(a.日期,4)
and cast(right(日期,2) as int)=cast(right(a.日期,2) as int)-1))
as 数据
from table1 a,table2 b
where a.日期='200402' and a.序号=b.序号Top
4 楼liuhui810(小苹果)回复于 2004-11-04 13:23:46 得分 0
to:lsxaa(小李铅笔刀)
子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。Top
5 楼liuhui810(小苹果)回复于 2004-11-04 13:32:11 得分 0
更改:table1的序号是table2的外健
两张表:
table1: 序号,日期, 数据 table2: 序号,属性
1 200401 10 1 ...
1 200402 20 2 ...
. . . . ...
在给定序号和日期的情况下,我想要得到这样的Sql:
select table2.序号,table2.属性, tbale1.日期,数据为200402和200401的差值(也就是20-10)
where 日期='200402' and table1.序号=table2.序号
怎样得到上面差值?这个sql怎么写?
Top
6 楼liuhui810(小苹果)回复于 2004-11-04 13:35:11 得分 0
to:netcoder(朱二)
我想要得到的数据集是:
table2.序号,table2.属性,tbale1.日期,所选月的数据-上个月的数据Top
7 楼luyajun(卢亚君)回复于 2004-11-04 13:49:39 得分 0
select b.序号,b.属性, a.日期,(a.数据-(select SUM(数据) from table1 where 日期=CAST(CAST(a.日期 AS INT)-1 AS VARCHAR(6))) 数据 from table1 a,table2 b where a.日期='200402' and a.序号=b.序号Top
8 楼lsxaa(小李铅笔刀)回复于 2004-11-04 13:51:37 得分 20
select b.序号,
b.属性,
a.日期,
(a.数据-(select top 1 数据 from table1 --加top 1
where left(日期,4)=left(a.日期,4)
and cast(right(日期,2) as int)=cast(right(a.日期,2) as int)-1))
as 数据
from table1 a,table2 b
where a.日期='200402' and a.序号=b.序号Top
9 楼liuhui810(小苹果)回复于 2004-11-04 14:19:17 得分 0
谢谢lsxaa(小李铅笔刀) 20分
版主揭帖Top




