Hi, try this in D4, it will give you records added. (counts rows with flag value =1 for every institution). Copy the function down through D23. To count other flag values, you have to change the =1 to 2 or 3.
'D4:
=SUMPRODUCT(('Records from SQL'!$D$2:$D$15737=1)*('Records from SQL'!$F$2:$F$15737=$C4))
PS have you considered just using a pivot table?