Why was David's census so unpleasant to our Creator? Was it a sin? If so, why? What is wrong in doing a census? There were previous censuses made and there were no problems.
Rashi to Exodus 30:12 explains that counting Jews directly incites the Evil Eye, which, in turn, causes a plague.
ולא יהיה בהם נגף. שֶׁהַמִּנְיָן שׁוֹלֵט בּוֹ עַיִן הָרָע, וְהַדֶּבֶר בָּא עֲלֵיהֶם, כְּמוֹ שֶׁמָּצִינוּ בִימֵי דָּוִד (שמואל ב כ"ד):
”That there not be calamity among them” - for the Evil Eye rules through counting, and a plague comes upon them, like we find in the days of David (Shmuel 2:24:1ff).
Unlike the census in the days of King David, the previous ones were done through half-Shekalim, as prescribed in the Exodus passage. That way, it was the coins being counted, not the people, and so the Evil Eye would not rule over them.
David decides to take a census
II Samuel, chapter 24, records one of the most puzzling stories in the Bible. II Samuel 24:9 tells us that King David took a census that resulted in a plague that killed seventy thousand Israelites. But counting or taking a census in the Bible could be done in order to determine the availability for war, for the division of land, and for tax purposes. But what did David do wrong and why were innocent people, indeed seventy thousand of them, killed by a plague?
In the story, David orders his commanding general Joab to assemble the soldiers to tally people. The procedure ends after nine months and twenty days, when Joab returns with the count of eight hundred thousand men, along with an additional five hundred thousand from Judah as reserves which could be mobilized in a time of war. When David heard this report he immediately began to regret ever having taken the census. For the count has been made public.
What did Maimonides think
In his Guide of the Perplexed 2:48, Maimonides explains that whenever the Bible says that G-d did something, it is not that G-d actually did it but what occurred was the result of the laws of nature. Since G-d created the laws of nature, the Bible attributes the event to G-d, since G-d is the ultimate cause. If we understand the text in this way, we can posit that G-d did not really become angry (Rambam felt that G-d does not have emotions).
Why did David take the census?
David probably undertook the census to gauge how many men or to procure potential soldiers that he could muster for an anticipated battle since David engaged in many wars. For the census, David dispatched his senior officers to determine who was fit for military service. However, the census was done publicly which came to the attention of Israel’s enemies, the very nations that David was assembling forces to fight. When the enemies learned of the census they took preventative measures and mapped out the area of drafting disseminated along Israel's boarder, resulting in an ambush of David’s forces.
After the census, David lamented it was too late. There was nothing he could do but face the consequences. The world functions according to the laws of nature, and one of those laws is that every act has a consequence. Thus, the prophet Gad had three possibilities for David’s negligent behavior. David could have fought the enemy or famine might arise as what happened to Saul. The plague was the result of a great slaughter [decomposing corpses]. Alternatively, the seventy thousand men were killed in battle, a “plague.” This number would have been much smaller had the census not been publicized. After the plague, Gad told David to set an altar where the plague has stopped, as a remembrance for David not to make the same mistake twice. The phrase “David’s Heart Smote Him” is a metaphor for David’s regret since the ancients considered the heart to be the seat of wisdom.
The last chapter of the biblical book of Samuel, if taken literally describes an angry G-d pushing David, resulting in deaths of tens of thousands. This is theologically problematic. But if we read according to Maimonides’ teaching in Guide 2:48, it can be explained in a more rational manner. David was punished due to natural consequences. When David dispatched a military contingent to take a census over a nine-month period which was mistakenly publicized. We can surmise that the enemy discovered the secret, fought or ambushed David’s army, resulting in the deaths of seventy thousand men, followed by a three-day plague.