应该给每间房子加一个状态,标示是否有人住不就可以了。
如果没有这样的表,可以用一下语句查询:
select distinct 房间号 from your_table where 退床时间 is null。另外,每个床号的居住时间可以用下面语句得出:
select (datediff(d,入住时间,退床时间) + (case when datepart(hh,入住时间)>12 then 0 else 1 end ) + (case when datepart(hh,退床时间)<12 then 0 else 1 end )) from your_table

解决方案 »

  1.   

    你加状态的方法很好啊。不过在将房屋标志为退房的时候,一定要查找一下,看看有没有该房间还没有退掉的床,这样就可以处理了。如果用SQL语句的话,会很那处理的。
      

  2.   

    select 房间号,
    case
     (select count(退房时间) from table b where b.退房时间<a.入住时间 and b.房间号=a.房间号)
    -(select count(入住时间) from table b where b.入住时间<a.入住时间 and b.房间号=a.房间号)
     when 0
     then 入住时间 end 入住时间,
    case
     (select count(退房时间) from table b where b.退房时间<=a.退房时间 and b.房间号=a.房间号)
    -(select count(入住时间) from table b where b.入住时间<=a.退房时间 and b.房间号=a.房间号)
     when 0
     then 入住时间 end 退房时间
    into #tmp
    from table aselect distinct 
     房间号,
     入住时间,
     case
      when 入住时间 not is null
      then (select min(退房时间) from #tmp b where b.退房时间>a.入住时间)
     end 退房时间
    from #tmp a
      

  3.   

    第1个select的第2个case的then后面应该是“退房时间”
    写错了。。
      

  4.   

    case
     (select count(退房时间) from table b where b.退房时间<a.入住时间 and b.房间号=a.房间号)
    -(select count(入住时间) from table b where b.入住时间<a.入住时间 and b.房间号=a.房间号)
     when 0
     then 入住时间 end 入住时间,是什么意思?