读书人

状态历史的有关问题

发布时间: 2012-02-23 22:01:36 作者: rapoo

状态历史的问题
状态历史表StatusHistory有Status,TransactionId,AsofDate.Status的取值范围包括(1,2,3,4).现在要查到某个日期比如2007-7-31 Status为1,2或者3的数据.
一个TransactionId会有多个Status不用游标能实现吗?

[解决办法]
--这样?
select * from tablename where status in(1,2,3) and AsofDate = '2007-7-31 '
[解决办法]
表中数据是什么样子?
[解决办法]
?


Select * From StatusHistory
Where Status In (1,2,3,4) And DateDiff(dd, AsofDate, '2007-7-31 ') = 0
[解决办法]
要查到某个日期比如2007-7-31 Status为1,2或者3的数据.
------------------------------------------------
楼主是指状态一直保持1/2/3其中的一种且不变化?

如果是这样,那么

select
s.*
from
StatusHistory s
where
s.Status in(1,2,3)
and
datediff(dd,s.AsofDate, '2007-07-31 ')=0
and
not exists(select 1 from StatusHistory where Status!=s.Status and datediff(dd,AsofDate,s.AsofDate)=0)

读书人网 >SQL Server

热点推荐