多条记录如何取最新的记录

ccsheng 2011-08-12 03:17:22
有两张表,第一张是销售订单表,第二章是价格表。价格表中同一个产品会有多条记录。如何把价格表左连接到销售订单表,取最新的价格?价格表例子如下:
ItemCode##StartDate##UnitPrice
12345##2011-1-1##1.5
12345##2011-5-1##1.7
12345##2011-8-1##1.8
希望取到第三条记录
谢谢。
...全文
133 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gw6328 2011-08-12
  • 打赏
  • 举报
回复
感觉这个问题怪怪的。销售记录里的去连出最新价格?
快溜 2011-08-12
  • 打赏
  • 举报
回复
select * from 
(select row_number() over(partition by a.订单id order by b.StartDate desc) no,*
from 订单表 a,价格表 b where a.订单id=b.订单id) t
where no=1
-晴天 2011-08-12
  • 打赏
  • 举报
回复
select a.*,b.* from 价格表 a left join 销售订单表 b on a.ItemCode=b.ItemCode
where not exists(select 1 from 价格表 where ItemCode=a.ItemCode and StartDate>a.StartDate)
chuanzhang5687 2011-08-12
  • 打赏
  • 举报
回复
select 价格 from 价格表 a left join 销售订单表 b on a.销售订单id = b.id
where not exist
(select 1 from 价格表 c where c.id = a.id and c.datetime>a.datetime )
--举个例子

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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