with tb1
as
(select ROW_NUMBER() over(partition by hid order by lid) as row_num, *
from test3
where [max] >= 8 and [current] >= 8 and ([state] = 1 or [state] = 0))
select * into #t from tb1 where row_num = 1;
select SUM([state]) as [Count], COUNT(*) as Alls, STR(ROUND(SUM([state]) * 100 / COUNT(*), 0)) + '%' as 'P'
from #t;
------解决方案--------------------
SQL code
select
sum(case when state=1 then 1 else 0 end) as [count]
,count(1) as [count]
,P=Left(sum(case when state=1 then 1 else 0 end)*1.0 /count(1)*1.0*100,5) +'%'
from B a where max>=8 and [current]>=8 AND state>=0
AND not exists ( select 1 from B where a.hid=hid AND a.lid>lid)
------解决方案--------------------