Select ID,sum(case when direction = 1 then num else 0 end) as INNum,
sum(case when direction = -1 then num else 0 end) as OUTNum
from 表 group by ID
sum(case when direction = -1 then num else 0 end) as OUTNum
from 表 group by ID
(select sum(num) where id = a.id and direction = -1) as OUTNum
from 表 a