我有一个连接到一个表,我想包括所有在特定日期后有记录的用户,但只包括计数中另一个日期之后的记录。基于数据子集的计数
这里是我的SQL:
select a.userid, count(ce.codeentryid)
from [account] a
inner join [profile] p
on a.userid = p.userid
inner join codesentered ce
on a.userid = ce.userid and ce.entrydate > '2011-01-01 00:00:00'
where a.camp = 0
group by a.userid
order by a.userid
所以在这里我要查看谁2011 1月1日之后,已经进入了一个代码的所有用户的名单,但只包括在计数码2013年1月1日之后进入我该怎么做?
编辑:所以这会给我在01/01/2011后输入代码的所有用户,但只包括01/01/2013之后输入的代码在计数?
select a.userid, count(CASE WHEN ce.entrydate > '2013-01-01 00:00:00' THEN 1 ELSE 0 END)
from [account] a
inner join [profile] p
on a.userid = p.userid
inner join codesentered ce
on a.userid = ce.userid and ce.entrydate > '2011-01-01 00:00:00'
where a.camp = 0
group by a.userid
order by a.userid
请分享样品数据和要求的输出。我无法理解你的要求。 – AK47 2014-12-04 11:43:02