请教高手,这个查询该怎么写???(加急!!)

zcflion 2002-03-26 11:27:12
现有数据库结构及数据如下(当然数据不止这些,数量大概在1千万条左右):
类别 时间 值
(tp char) (tm datetime) (vl numeric)
CGD020023 2002-3-21 23:51:35 305.539
CGD020023 2002-3-21 23:56:59 544.495 (*)
CGD020023 2002-3-22 0:02:24 604.161
CGD020023 2002-3-22 0:50:59 215.099
CGD020023 2002-3-22 0:56:21 125.712 (*)
CGD020023 2002-3-22 1:01:44 510.013
CGD020023 2002-3-22 1:07:08 382.672
CGD020023 2002-3-22 1:55:40 361.694
CGD020023 2002-3-22 2:00:04 5.511 (*)
CGD020024 2002-3-21 23:51:35 319.784
CGD020024 2002-3-21 23:56:59 45.972 (*)
CGD020024 2002-3-22 0:02:24 299.963
CGD020024 2002-3-22 0:50:59 641.163
CGD020024 2002-3-22 0:56:21 531.216
CGD020024 2002-3-22 1:00:44 442.937 (*)
CGD020024 2002-3-22 1:07:08 487.65
CGD020024 2002-3-22 1:55:40 479.904 (*)
CGD020024 2002-3-22 2:01:04 327.103
要按(类别)找出(时间)离整点最近(往前推)的记录,值是未知的
即:tm<=整点 每个类别每个整点一条

就是在上面一堆数据中找出标(*)的数据
...全文
45 60 打赏 收藏 转发到动态 举报
写回复
用AI写文章
60 条回复
切换为时间正序
请发表友善的回复…
发表回复
nononono 2002-03-27
  • 打赏
  • 举报
回复
我给一个可运行的语句:

select a.*
from table1 as a,
( select tp,tm1,max(tm) as tm
from (select tp, convert(char(13),DATEADD(mi,-1,tm),120) as tm1, tm from table1) as c
group by tp,tm1) as b
where a.tp=b.tp and a.tm=b.tm
baresi 2002-03-27
  • 打赏
  • 举报
回复
楼上对的,我测过了
jamex 2002-03-27
  • 打赏
  • 举报
回复
我想了很久,终于得出了一下sql语句,我上机调试过了,应该没有什么问题了!!大家看看!

终极答案:
假设表名为tbldata

Select tbldata.* from tbldata,
(
select tp,
MAX(tm) as MaxTm
from tbldata
group by tp,
ltrim(rtrim(cast(Year(DATEADD(mi,-1,tm)) as char(4))))
+right('00'+ltrim(rtrim(cast(Month(DATEADD(mi,-1,tm)) as char(2)))),2)
+right('00'+ltrim(rtrim(cast(Day(DATEADD(mi,-1,tm)) as char(2)))),2)
+right('00'+ltrim(rtrim(cast(datepart(hour,DATEADD(mi,-1,tm)) as char(2)))),2)

) as tbldata1
Where tbldata.tm=tbldata1.MaxTm And tbldata.tp=tbldata1.tp
order by tblData.tp,tblData.tm
szhuk 2002-03-27
  • 打赏
  • 举报
回复
不好意思刚才测试一下才知道有问题:最好减30然后取绝对值,按降次排列,绝对么又问题
程序如下:
Select ID,upfiledate,
ABS(datepart(minute,upfiledate) - 30) as test1,
ABS(datepart(second,upfiledate) - 30) as test2
from FileSetUp order by test1 desc,test2 desc
其中FileSetUp为测试表名,upfiledate为时间列
zcflion 2002-03-27
  • 打赏
  • 举报
回复
谢谢大家!
zcflion 2002-03-27
  • 打赏
  • 举报
回复
这个是最终的结果!!

select T.* from st_river_r0_t T,
(select stcd,max(ymdhm) ymdhm from st_river_r0_t
group by stcd,convert(char(8),DATEADD(second, -1,ymdhm),112),
datepart(hour,DATEADD(second, -1,ymdhm))) T1
where T.stcd=T1.stcd and T.ymdhm=T1.ymdhm

我在大家的帮助下终于搞定了!!

呵呵开心!!呆会儿结贴!!

不过可惜的是,我弄了这么久老板现在说,不用做了

数据库是空的!!??吐血!!!
szhuk 2002-03-27
  • 打赏
  • 举报
回复
有没有人把时间字段的数值取出来,分别取出分和秒的值,经过简单处理在按这两个的值排序(取出分和妙的值 - 60 然后取绝对值,这样不是约靠近正点的数值越小么?)我没有时间试验,就这样吧
alen_fen 2002-03-27
  • 打赏
  • 举报
回复
厉害啊!
jamex 2002-03-26
  • 打赏
  • 举报
回复
正确的应该是这个!

Select * from 表,
(
select tp,MAX(tm) as MaxTm
from 表
group by tp, cast(Year(tm),char(4))+'-'+cast(Month(tm),char(4))
+'-'+cast(Days(tm),char(4)+' '+cast(Hour(tm),char(4))+':00:00'
) as 表1
Where 表.tm=表1.MaxTm And 表.tp=表1.tp

xhuangp 2002-03-26
  • 打赏
  • 举报
回复

select tp ,max(datepart(minute,tm)),vl from yourtable group by tp,datepart(Hour,tm)
zcflion 2002-03-26
  • 打赏
  • 举报
回复
我写一个错误的SQL你就看得出来!
select tp,max(tm),vl(这个值为max TM时的值!)
from 表
where tm<='2002-3-3 20:00'
group by tp
x_zing 2002-03-26
  • 打赏
  • 举报
回复
select t.* from tablename t join
(select 类别,min(datediff(minute,时间,convert(smalldatetime,
convert(varchar,datepart(year,时间))+'-'+
convert(varchar,datepart(month,时间))+'-'+
convert(varchar,datepart(day,时间))+' '+
convert(varchar,datepart(hour,时间)+1)+':00:00'))) as minrecord
from tablename
group by 类别) t2 --保存时间最合适的记录
on --当类别和时间都符合时将其选择出来
t.类别=t2.类别 and
datediff(minute,t.时间,convert(smalldatetime,
convert(varchar,datepart(year,t.时间))+'-'+
convert(varchar,datepart(month,t.时间))+'-'+
convert(varchar,datepart(day,t.时间))+' '+
convert(varchar,datepart(hour,t.时间)+1)+':00:00'))=t2.minrecord

如果还要准确一点,可以将datediff中的minute改成second。
zcflion 2002-03-26
  • 打赏
  • 举报
回复
高手们,帮忙看一看啊!
xhfjy 2002-03-26
  • 打赏
  • 举报
回复
看不出什么来:)
说清楚你的筛选条件
lincanwen 2002-03-26
  • 打赏
  • 举报
回复
SELECT vl from 表 where substring(format(tm,"yyyy-mm-dd hh:mm:ss"),1,13)=max(distinct(substring(format(tm,"yyyy-mm-dd hh:mm:ss"),1,13)))
lyxinfo 2002-03-26
  • 打赏
  • 举报
回复
胡涂了,下边的那个是到分钟的,都考虑了整点的情况。
不过这样写Sql的执行效率,看来不会好。:(
看看谁还有好的。
lyxinfo 2002-03-26
  • 打赏
  • 举报
回复
哦,我测试的表名是TT。
lyxinfo 2002-03-26
  • 打赏
  • 举报
回复
呵呵,下午写的那个没有整理,见笑了,如果完全精确,这个应该可以。
select t1.*
from tt t1 ,(select tp as tp ,max(tm) as tm
from tt
group by tp,case
when convert(datetime ,convert(char(13),tm,120)+ ':00:00') = tm
then tm
else convert(datetime ,convert(char(13),dateadd(hh,1,tm),120)+ ':00:00') end) t2
where t1.tp = t2.tp and t1.tm = t2.tm
ORDER BY T1.TP
精确到秒这样写,但是最后一条实在不知道该怎样处理了。比你的结果多一条。
select t1.*
from tt t1 ,(select tp as tp ,max(tm) as tm
from tt
group by tp,case
when convert(char(13),tm,120)+ ':00' = convert(char(16),tm,120)
then convert(char(16),tm,120)
else convert(char(13),dateadd(hh,1,tm),120)+ ':00' end ) t2
where t1.tp = t2.tp and convert(char(16),t1.tm,120) = convert(char(16),t2.tm,120)
ORDER BY T1.TP,t1.tm
sgb 2002-03-26
  • 打赏
  • 举报
回复
都是高手哇!!
请关注小弟提出的问题?
已贴上?
jamex 2002-03-26
  • 打赏
  • 举报
回复
to zcflion(土匪):

回复人: zcflion(土匪) ( ) 信誉:100 2002-3-26 14:06:19 得分:0
to :jamex(1 + 1 = 爱)

大哥这个思路是正确的,只是不要精确到秒上可不可以,只要到分钟
也就是如果2002-3-3 2:00:45有数据的话,就只要这条了!!
而不要2002-3-3 1:59:59的数据!


再试试这个!

Select * from 表,
(
select tp,MAX(tm) as MaxTm
from 表
group by tp, cast(Year(DATEADD(mi,-1,tm)),char(4))+cast(Month(DATEADD(mi,-1,tm)),char(2))
+cast(Day(DATEADD(mi,-1,tm)),char(2)+' '+cast(datepart(hour,DATEADD(mi,-1,tm)),char(2))
) as 表1
Where 表.tm=表1.MaxTm And 表.tp=表1.tp



加载更多回复(40)

34,873

社区成员

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

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