SQL SERVER查询问题(100)

  • 主题发起人 主题发起人 abcls
  • 开始时间 开始时间
A

abcls

Unregistered / Unconfirmed
GUEST, unregistred user!
有一表如下:Employeeid Bedid StartDate EndDate 1 1 2010.08.01 2010.09.0110 1 2010.09.10 2010.10.122 3 2010.03.02 2010.05.11155 3 2010.05.05 2010.08.22我想列出每个bedid的最大的startdate的记录.如下:Employeeid Bedid StartDate EndDate10 1 2010.09.10 2010.10.12155 3 2010.05.05 2010.08.22
 
试试下面的语句(我是Oracle):Select a.Employeeid, a.Bedid, a.StartDate, a.EndDate From 表 a Left Join (Select Bedid, Max(StartDate) StartDate, Max(EndDate) EndDate From 表 Group By Bedid) b On a.Bedid = b.Bedid and a.StartDate = b.StartDate and a.EndDate = b.EndDate Order By a.Employeeid
 
Bedid ,startDate 值是不是有可能重复,前面能不能加个自增字段id;select * from test3 where id in (select myid from ( select max(id) as myid , bebid,max(startdate) as b from test3 group by bebid) c )
 
改为下面的语句试试:Select a.Employeeid, a.Bedid, a.StartDate, a.EndDate From 表 a, (Select Bedid, Max(StartDate) StartDate, Max(EndDate) EndDate From 表 Group By Bedid) b Where a.Bedid = b.Bedid and a.StartDate = b.StartDate and a.EndDate = b.EndDate Order By a.Employeeid;
 
select Bedid,max(StartDate) from 表 group by Bedid
 
select * from kk a where StartDate = (select Max(StartDate) from kk where BedId = a.BedId)
 
后退
顶部