The simple answer is there is no simple answer.
The theory was that if the B17's stayed in tight formation, the covering fire from all of the bombers would make for the best defensive position. Unfortunately for them, no one told the German pilots.
One good hit from a cannon shell could slow down a bomber and it would almost inevitably fall out of formation where it would be at its most vulnerable. In 1942 to 1943 the P38's P47's and Spitfires couldn't escort the bombers into Germany, they hadn't the range, so the bombers were vulnerable throughout their time over Germany. The introduction of the P51D altered that.
The German tactics varied, but one of the most effective, if dangerous, was the head on attack where fewer American guns could be brought to bear. One high speed pass, determinedly struck, could be devastating. High speed 'hit and run' attacks from above also took a heavy toll of the bombers. The 109's and 190's would attack from above, open fire when in range, get out of range as quick as possible and if they had any ammunition left,they would climb for another pass.
The Germans also used proximity rockets,which were fired at bomber formations from a 'safe' distance and exploded close to the bombers with similar results to accurate flak. Although frighteningly devastating if they were used and properly worked, it was not the most effective weapon.
Talking of flak, the German flak was superior by far to any allied anti aircraft artillery until the last year of the war. The impressive 88mm AA gun with accurate and reliable proximity fuses, especially when radar guided, made it a deadly weapon.
Once a bomber was out of formation over Germany, very few survived the predations of flak or fighters.