一个SQL语句,解决即可结帖解决办法

一个SQL语句,解决即可结帖
表字段   值(decimal)   时间(dateTime)
要求是   求出一个列   显示出与最近的上一个时间数据相比,值的状态.如涨   平   落
比如数据   19.20   2007-03-01   13:00:00
19.40   2007-03-01   14:00:00
 
求出是   平
              涨

------解决方案--------------------
不太明白,说清楚一点撒

------解决方案--------------------
你所谓的最近是怎么算的
------解决方案--------------------
select case when 值 > (select top 1 值 from 表 b where b.時間 <a.時間 order by 時間 desc) then '漲 ' when 值 =(select top 1 值 from 表 b where b.時間 <a.時間 order by 時間 desc) then '平 ' else '落 ' from 表a
------解决方案--------------------
--創建測試環境
Create Table 表
(值 decimal(10, 2),
时间 dateTime)
Insert 表 Select 19.20, '2007-03-01 13:00:00 '
Union All Select 19.40, '2007-03-01 14:00:00 '
GO
--測試
Select
A.值,
A.时间,
(Case When A.值 - IsNull(Max(B.值), 0) > 0 Then N '涨 ' When A.值 - IsNull(Max(B.值), 0) = 0 Then N '平 ' Else N '落 ' End) As 状态
From
表 A
Left Join
表 B
On A.时间 > B.时间
Group By
A.值,
A.时间
GO
--刪除測試環境
Drop Table 表
--結果
/*
值 时间 状态
19.20 2007-03-01 13:00:00.000 涨
19.40 2007-03-01 14:00:00.000 涨
*/
------解决方案--------------------
jydqwe(闲庭漫步) ( ) 信誉:100 2007-07-30 16:30:20 得分: 0


有点问题 就是并没有表b 一共只有一张表
是求出离记录时间最近的记录 并进行比较 如果不存在 就填写平.


-----------

你測試了沒?

我都寫出例子來了。

沒說你有表B,那裡的B是別名。

有點出入的是,如果不存在,那個語句填的是漲。
------解决方案--------------------
晕哦,鱼
只有两行数据测试

Select
A.值,
A.时间,
Case When B.值 is null then N '平 ' when A.值 - B.值 > 0 Then N '涨 ' When A.值 =B.值 Then N '平 ' Else N '落 ' End As 状态
From
表 A
Left Join
表 B
On B.时间=(select max(时间) from 表 where 时间 <a.时间)
order By
A.时间

------解决方案--------------------
declare @a table(id int,name decimal(6,2),lasttime datetime)
insert into @a select 1,19.20, '2007-03-01 13:00:00 ' union all
select 2,19.40, '2007-03-01 14:00:00 ' union all
select 3,19.50, '2007-03-01 15:00:00 '


select a.*,
case when (a.name - b.name)> 0 then N '涨 '
when (a.name - isnull(b.name,a.name))=0 then N '平 '
else N '跌 'end
from @a a
left join @a b on a.id = b.id +1
--结果,多加了id列,可以加一个临时表加这个列
id name lasttime
----------- --------------------------------------- ----------------------- ----
1 19.20 2007-03-01 13:00:00.000 平
2 19.40 2007-03-01 14:00:00.000 涨
3 19.50 2007-03-01 15:00:00.000 涨
------解决方案--------------------
修改下

--創建測試環境
Create Table 表
(值 decimal(10, 2),
时间 dateTime)
Insert 表 Select 19.20, '2007-03-01 13:00:00 '
Union All Select 19.40, '2007-03-01 14:00:00 '
GO
--測試
Select