这个应该怎么解决呀?

hundana5566 2006-09-20 06:19:55
表a


DateAndTime val
2006-8-10 22:22:00 321.567
````````````````````
2006-8-18 00:01:10 252.364
2006-8-18 00:30:15 0
2006-8-18 01:31:25 232.876
2006-8-18 02:30:20 0
2006-8-18 03:31:22 290.872
2006-8-18 03:31:22 252.872
```````````````````````````

每天想吧昨天24小时的数据送到b表(DateAndTime 主键)的c列

DateAndTime a b c
2006-8-18 00:00:00 356 1 321.567
2006-8-18 01:00:00 256 3 232.876
2006-8-18 02:00:00 440 18 0
2006-8-18 03:00:00 562 0 290.872

条件如下:
每个小时只能选取一条数据.假如这个数据有几条的话选那条最大的,假如相等选前面那条数据.再送到b表相对应的列上的时间行
...全文
87 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Well 2006-09-20
  • 打赏
  • 举报
回复
楼主不好意思,我dateadd()函数写错了,
现在订证。。
insert into b
select * from a where convert(char(10),DateAndTime ,120)=convert(char(10),dateadd(dd,-1,getdate()),120)
hundana5566 2006-09-20
  • 打赏
  • 举报
回复
服务器: 消息 174,级别 15,状态 1,行 1
dateadd 函数要求有 3 个参数。

hundana5566 2006-09-20
  • 打赏
  • 举报
回复
应该怎么更新b表的数据我写的是插入~
hundana5566 2006-09-20
  • 打赏
  • 举报
回复
select identity(int,1,1) as id,a into #1 from b
select identity(int,1,1) as id,b into #2 from b
select identity(int,1,1) as id,val into #2 from a where convert(char(10),DateAndTime ,120)=convert(char(10),dateadd(-1,getdate()),120)


insert into b(a,b)and a(val) select b.a,b.b from #1 a,#2 b,#3 c where a.id=b.id a.id=c.id

drop table #1,#2,#3
Well 2006-09-20
  • 打赏
  • 举报
回复

insert into b
select * from a where datediff(dd,DateAndTime ,getdate())=1
Well 2006-09-20
  • 打赏
  • 举报
回复
insert into b
select * from a where convert(char(10),DateAndTime ,120)=convert(char(10),dateadd(-1,getdate()),120)
hundana5566 2006-09-20
  • 打赏
  • 举报
回复
a和b的是别的时候插入的
hundana5566 2006-09-20
  • 打赏
  • 举报
回复
哦,不是象上一行,对不起.0点的数据没给出来
hundana5566 2006-09-20
  • 打赏
  • 举报
回复
看串啦,应该所有的数据向上一行00点的去掉
pengda1i 2006-09-20
  • 打赏
  • 举报
回复
估计:

update b
set c=(select max(val) from a where DateAndTime>=b.DateAndTime and DateAndTime<dateadd(hour,1,b.DateAndTime))
from b
where datediff(day,DateAndTime,getdate())=1
pengda1i 2006-09-20
  • 打赏
  • 举报
回复
a b 列怎么来
c列第一行的321.567怎么来?

34,588

社区成员

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

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