日期:2014-05-18 浏览次数:20607 次
select id,Count(id) as LoginT from A
inner join B on
id=aid
group by id
if OBJECT_ID('ta') is not null Drop table ta;
if OBJECT_ID('tb') is not null Drop table tb;
go
create table ta(id int, name varchar(16));
create table tb(Aid int, LoginTime varchar(14));
go
insert into ta(id, name)
select 526, 'aaa' union all
select 527, 'bbb' union all
select 528, 'bbb';
insert into tb(Aid, LoginTime)
select 526, '20120404202903' union all
select 526, '20120406202933' union all
select 527, '20120406203006'
select id, sum(ct) as [count]
from (
select ta.*, (case when aid is null then 0 else 1 end) as ct
from ta
left join tb
on id = aid
) a
group by id