寻求以SQL语句
问题: 假设有两个月的数据,查询最近一个星期是否出现最小值(此最小值是所有数据中的最小值)
数据库中每天仅有一个数据,无重复。
如
date value
2012-11-18 13
2012-11-17 11
2012-11-16 14
2012-11-15 16
2012-11-14 18
2012-11-13 12
2012-11-12 19
...
2012-09-20 8
2012-09-19 5
2012-09-18 16
[最优解释]
if exists(select * from tb as a where date between getdate()-7 and getdate()
and not exists(select * tb where value<a.value))
select 'yes'
else
select 'no'
[其他解释]
if (select min(value) from TB)=(select min(value) from TB where [date]>dateadd(day,-7,getdate())
select 'YES'
else
select 'NO’
[其他解释]
IF EXISTS (SELECT *
FROM tb AS a
WHERE DATE BETWEEN Dateadd(DAY,-7,Getdate())
AND Getdate()
AND NOT EXISTS (SELECT *
FROM tb
WHERE VALUE < a.VALUE))
PRINT 'yes'
ELSE
PRINT 'no'
[其他解释]
select
top 1 * from test where datediff(dd,[date],getdate()) between 0 and 7
and [value]=(select min([value]) from test)
order by [value] asc
[其他解释]
多谢各位数据库牛人了,我采用了2楼的方法。
[其他解释]
2楼的方法真的高明,小小数据库语句大道理,数据库语句其实就是算法的实现,3楼真不好意思,你的代码与2楼是一样的。