使用 SQL 中的子查询更新多行
我有一个汇总表从主表返回数据。我正在尝试从主表中的值更新一些摘要数据,如下所示:
update #summary
set TopSpeed = CD.TopSpeed, SpeedTime = CD.TimeSent, SpeedDriver = CD.Driver
from
(
select top 1 TopSpeed, TimeSent, Driver
from CarData
where CarData.VehicleId = #summary.VehicleId
and CarData.TimeSent between #summary.Start and #summary.Stop
order by CarData.TopSpeed desc, TimeSent desc
) as CD
#summary 临时表正在创建有关汽车行程的汇总数据。每辆车可以有多个行程,每个行程都有开始和停止时间。 CarData 表包含所有详细的汽车数据,如速度和位置等。
请问您能帮忙吗?
谢谢, 罗伯特
I have a summary table return data from a master table. I am trying to update some of the summary data from values in the master table as such:
update #summary
set TopSpeed = CD.TopSpeed, SpeedTime = CD.TimeSent, SpeedDriver = CD.Driver
from
(
select top 1 TopSpeed, TimeSent, Driver
from CarData
where CarData.VehicleId = #summary.VehicleId
and CarData.TimeSent between #summary.Start and #summary.Stop
order by CarData.TopSpeed desc, TimeSent desc
) as CD
The #summary temporary table is creating summarised data about trips undertaken by a car. There can be multiple trips for each car with each trip having a start and stop time. The CarData table contains all the detailed car data like speed and position etc.
Please can you help?
Thanks,
Robert
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
Try this:
这里有一种方法可以做到这一点,尽管我不喜欢它执行 3 次子查询。这是我希望有人有更好主意的案例之一。
Where here's a way to do it, though I don't like it doing the subquery 3 times. It's one of those cases where I hope someone has a better idea.