As Derrick said:select (po.org_id) as [orgid],convert(datetime, convert(varchar(13), lh.login_history_dt, 120) + ':00:00.000') as time,count(*) as [number of log-in]
into #login_historyfrom login_history lhinner join login l on l.login_id = lh.login_idinner join p_org po on po.p_id = l._p_idinner join p_login pl on pl.login_id = lh.login_idinner join p_account pa on pa.p_id = pl.p_idwhere login_history_dt between '20000101' and '20040708' group by po.org_id,convert(datetime, convert(varchar(13), lh.login_history_dt, 120) + ':00:00.000')order by po.org_id,count(*) desc, convert(datetime, convert(varchar(13), lh.login_history_dt, 120) + ':00:00.000')select orgid, (select [time] from #login_history where orgid = o.orgid and [number of log-in] = max(o.[number of log-in])),max([number of log-in])from #login_history ogroup by orgid
--KenYour Kung-Fu is not strong. -- 'The Core'