求一个SQL语句

hzcao 2003-10-16 11:16:15
RoomPrice
RoomNO---RoomType----现价----启用时间
101------标准间-------160----2003-9-21
101------标准间-------220----2003-10-1
201------标准间-------250----2003-9-30

ReservationState
RoomNO---预定时间
101------2003-10-3
201------2003-10-1

产生这样的查询:
RoomNO---RommType---预定时间------房价
101------标准间------2003-10-3----220
210------标准间------2003-10-1----250

我需要启用时间<预定时间,且与预定时间最近的一条。
...全文
28 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hzcao 2003-10-16
  • 打赏
  • 举报
回复
谢谢楼上的高手,呵呵
hzcao 2003-10-16
  • 打赏
  • 举报
回复
第1楼和第2楼的肯定不对哟,呵呵
第3楼的偶正在考虑
zjcxc 元老 2003-10-16
  • 打赏
  • 举报
回复
测试结果

RoomNO RoomType 预定时间 房价
----------- ---------- ---------- -----------
101 标准间 2003-10-03 220
201 标准间 2003-10-01 250

(所影响的行数为 2 行)
zjcxc 元老 2003-10-16
  • 打赏
  • 举报
回复
--下面是数据测试

--数据测试环境
declare @RoomPrice table(RoomNO int,RoomType varchar(10),现价 int,启用时间 datetime)
insert into @roomprice
select 101,'标准间',160,'2003-9-21'
union all select 101,'标准间',220,'2003-10-1'
union all select 201,'标准间',250,'2003-9-30'

declare @ReservationState table(RoomNO int,预定时间 datetime)
insert into @ReservationState
select 101,'2003-10-3'
union all select 201,'2003-10-1'

--查询语句
select a.RoomNO,a.RoomType,预定时间=convert(varchar(10),b.预定时间,120),房价=a.现价
from @RoomPrice a inner join @ReservationState b on a.RoomNO=b.RoomNO
where a.启用时间<b.预定时间
and a.启用时间=(select max(启用时间) from @RoomPrice where RoomNO=a.RoomNO)
order by a.RoomNO
zjcxc 元老 2003-10-16
  • 打赏
  • 举报
回复
select a.RoomNO,a.RoomType,b.预定时间,房价=a.现价
from RoomPrice a inner join ReservationState b on a.RoomNO=b.RoomNO
where a.启用时间<b.预定时间
and a.启用时间=(select max(启用时间) from RoomPrice where RoomNO=a.RoomNO)
yujohny 2003-10-16
  • 打赏
  • 举报
回复
修正:
select RoomNo,预定时间,(select top 1 RoomType,现价 as 房价 from RoomPrice where RoomNo=A.RoomNo and 启用时间 <=预定时间 order by 启用时间 desc) from ReservationState A
yujohny 2003-10-16
  • 打赏
  • 举报
回复
select RoomNo,预定时间,(select top 1 RoomType,现价 as 房价 from RoomPrice where RoomNo=A.RoomNo and 启用时间 <=预定时间) from ReservationState A

34,575

社区成员

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

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