There are two accounts of David’s census:
2 Samuel 24:1 Once again the anger of the Lord burned against Israel, and he caused David to harm them by taking a census. ‘Go and count the people of Israel and Judah,’ the Lord told him.
And
1 Chronicles 21:1 Satan rose up against Israel and caused David to take a census of the people of Israel.
So who prompted David to take this census? A similar situation is in Job, where Satan caused Job’s calamities, but tried to blame it on God for permitting it.
Young’s literal translation brings the concept together quite well:
2 Samuel 24:1 And the anger of Jehovah addeth to burn against Israel, and [an adversary] moveth David about them, saying, `Go, number Israel and Judah.’
Taking a census wasn’t, in itself, a wrong thing to do. Moses did it (Numbers 1:1-4, 4:2, 4:22); Saul did it (1 Samuel 11:8), and even David had done it before (2 Samuel 18:1).
But this one wasn’t a good idea. It wasn’t commanded by God; it wasn’t necessary; it was prompted by Satan.
Even Joab knew it was a bad idea:
2 Samuel 24:3But Joab replied to the king, ‘May the Lord your God let you live to see a hundred times as many people as there are now! But why, my lord the king, do you want to do this?’
But it went ahead.
Almost certainly, the atonement ‘tax’ wasn’t collected along the way:
Exodus 30:12-13 Whenever you take a census of the people of Israel, each man who is counted must pay a ransom for himself to the Lord. Then no plague will strike the people as you count them.
Even if the census itself hadn’t been a problem, the failure to make sure the atonement tax of half a shekel was (the money was to be used for the care of the Tabernacle).
Interestingly, a three-day plague was one of the consequences David could choose, or a three-year famine, or a three-month loss to enemy forces (the three indicates a divine completion). David chose plague:
1 Chronicles 21:13But let me fall into the hands of the Lord, for his mercy is very great. Do not let me fall into human hands.
Was it fair that the whole of Israel suffered for David’s sin? Well, yes. It was Israel’s sin that had caused the problem in the first place. It reinforces the idea that we do get the leaders we deserve, or to put it anther way, that a king can’t go too far ahead of his people.