.header on
.mode column
.output 4.txt
select 
  substr(game_id,4,3) as decade
 ,inn_ct as inning
 ,bat_home_id as half
 ,sum(case when ab_fl = 'T' then 1 else 0 end) as AB
 ,sum (case when h_cd > 0 then 1 else 0 end) as H 
 ,round((sum(case when h_cd > 0 then 1 else 0 end)*1.0)/(sum (case when ab_fl = 'T' then 1 else 0 end)*1.0),3) as BA
from events 
where 
  inn_ct < 3
group by 
  decade
 ,inning
 ,half
;

