求一个查询语句!

Rex_love_Burger 2010-07-26 12:20:39
有如下数据表:
STCD varchar
DYP decimal
TM datetime

请问如何查询某STCD对应的,每天(该表中包含的所有日期)中离上午8点最近的一条数据的DYP的值?

十分感谢!
...全文
132 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Rex_love_Burger 2010-07-27
  • 打赏
  • 举报
回复
谢谢大家,非常感谢,结贴
情殇无限 2010-07-26
  • 打赏
  • 举报
回复
select *  from (
Select stcd,dyp,tm,rank()
over(partition by stcd ,convert(varchar(10),tm,120) order by datediff(minute,tm,convert(varchar(10),tm,120)+' 08:00:00') desc)
rn from #tb) x
where abs(rn)=1
情殇无限 2010-07-26
  • 打赏
  • 举报
回复
select dyp  from (
Select stcd,dyp,tm,rank()
over(partition by convert(varchar(10),tm,120) order by datediff(minute,tm,convert(varchar(10),tm,120)+' 08:00:00') desc)
rn from #tb) x
where abs(rn)=1

排序错了
情殇无限 2010-07-26
  • 打赏
  • 举报
回复

select dyp from (
Select stcd,dyp,tm,rank()
over(partition by convert(varchar(10),tm,120) order by datediff(minute,tm,convert(varchar(10),tm,120)+' 08:00:00'))
rn from 表) x
where abs(rn)=1
水族杰纶 2010-07-26
  • 打赏
  • 举报
回复
--try
select *
from tb t
where TM=(select top 1 tm
from tb
where STCD=t.STCD
order by abs(datediff(hour,tm,convert(varchar(10),getdate(),120)+' 08:00:00')))
mxbhot85 2010-07-26
  • 打赏
  • 举报
回复
select *
from tbl_date
where stcd = ( select top 1 stcd
from tbl_date t
order by abs(datename(hh,t.tm)*60+datename(mi,t.tm)-480))

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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