日期:2014-05-19  浏览次数:20418 次

关于一个很牛的SQL语句求教
769001   6 商务双床套房 469.0000 228.0000 2007-02-01   17:35:30.060
769001   5 商务大床套房 469.0000 228.0000 2007-02-01   17:34:23.107
code       roomid
769001   4 豪华标准房 439.0000 198.0000 2007-02-01   17:33:19.873
769001   3 标准房 409.0000 148.0000 2007-02-01   17:32:15.890
769001   4 豪华标准房 409.0000 198.0000 2007-02-01   17:31:13.793
769001   3 标准房 409.0000 148.0000 2007-02-01   17:30:11.107
769001   2 商务大床房 389.0000 188.0000 2007-02-01   17:24:53.093
769001   1 大床房 369.0000 168.0000 2007-02-01   17:23:49.483

现在数据表是这样
我要取出唯一的ROOMID号   根据最大的时间选

------解决方案--------------------
select top 1 roomid from table where code in (select distinct code from table) order by time desc
------解决方案--------------------
select max(时间),roomid from table_test group by roomid
------解决方案--------------------
你的数据得有个唯一ID,否则无法
------解决方案--------------------
create table tb
(
Code int,
RoomID int,
RoomName nvarchar(128),
Price decimal(18,4),
CheckDate DateTime,
)
----借用Zine_Alone的表结构,语句如下:

select * from tb A where not exists (select 1 from tb where RoomID=A.RoomID and CheckDate> A.CheckDate)
------解决方案--------------------
select * from tb A where not exists (select 1 from tb where RoomID=A.RoomID and CheckDate> A.CheckDate)

真没办法:上面语句,把两个 "tb "改成你的表名, "CheckDate "改成你的日期列名,就可以了.那儿用到临时表了?

------解决方案--------------------
象这样的 只是取得所有的不唯一的ROOMID
也就是说可能存在多条相同时间的roomid是吗?

如果定义了 code的话 有的就取不到了
这句话什么意思?