我想在SQL中编写一个查询,查看X号人员签到的日期是什么。记录正在跟踪此人的姓名,签入日期和签出日期(如果未签出则为空) )。
对于以下数据集,我怎么知道我在2/8时达到了3个患者限制?
Name, Date In, Date Out
John, 1/25/2014, NULL
Tom, 2/8/2014, 2/9/2014
Joe, 1/21/2014, 1/22/2014
Dave, 2/7/2014, NULL
到目前为止我有这个查询,但我不知道如何在用户签出时减少计数
select count(c1.Name) as count, c2.DateIn
from customer c1 join customer c2
on c1.DateIn <= c2.DateIn
group by c2.DateIn
having count(c1.Name) >= 3
谢谢!
答案 0 :(得分:2)
怎么样:
select t1.*,
(select count(t2.name) from customer t2
where t2.date_in <= t1.date_in and (t2.date_out is null or t2.date_out >= t1.date_in))
from customer t1
order by t1.date_in
新列显示了customer表中每一行的登录用户数。
答案 1 :(得分:0)
如果我正确理解了这个问题:
select date_in
from customer
where date_out is null
group by date_in
having count(*) >= 3