这跟上一个差不多吧
SELECT *
FROM TEST
WHERE A IN
(SELECT T1.A
FROM (SELECT A, COUNT(B) CNT FROM TEST GROUP BY A) T1, TEST_1 T2
WHERE T1.A = T2.A
AND T1.CNT > (T2.D - T2.C))
select pid,
sum(case when type=0 then 1 else 0 end) type_0,
sum(case when type=1 then 1 else 0 end) type_1,
sum(case when type=2 then 1 else 0 end) type_2
from A
group by pid