寻求以SQL语句

liuxmzc 2012-11-18 11:49:27
问题: 假设有两个月的数据,查询最近一个星期是否出现最小值(此最小值是所有数据中的最小值)

数据库中每天仅有一个数据,无重复。


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
...全文
126 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cxmcxm 2012-11-19
  • 打赏
  • 举报
回复
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'
开启时代 2012-11-19
  • 打赏
  • 举报
回复
if (select min(value) from TB)=(select min(value) from TB where [date]>dateadd(day,-7,getdate()) select 'YES' else select 'NO’
liuxmzc 2012-11-19
  • 打赏
  • 举报
回复
2楼的方法真的高明,小小数据库语句大道理,数据库语句其实就是算法的实现,3楼真不好意思,你的代码与2楼是一样的。
引用 2 楼 cxmcxm 的回复:
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'
liuxmzc 2012-11-19
  • 打赏
  • 举报
回复
多谢各位数据库牛人了,我采用了2楼的方法。
  • 打赏
  • 举报
回复
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
以学习为目的 2012-11-19
  • 打赏
  • 举报
回复
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'

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧