In the bible god seems a bit wishy washy when it comes to the census. David took a census and god got pretty angry by any standards. He murdered 70,000 people.
But when his son Solomon took a census of immigrants to Israel, god gave his implied approval by not murdering 70,000 people.
Indeed, in Numbers 26 god actually orders Moses to take a census. So that leaves us with three possibilities: 1) God wants us to take a census, 2) god is nonchalant towards the census, and 3) god abhors and responds with mass murder when we take a census.
If god exists it must be either number 1 or 2 because every state, regime, military, church, employee roster, and every other social institution have some form of a census. And yet, God has not said a word!
I sat down to read 2 Samuel 24 and was amazed at the wisdom expressed in that tale of woe.
One key bit of wisdom is how others besides ourselves suffer when we commit evil.
And this is particularly true of leaders of men like King David.
And again there are no contradictions in the scripture you quoted.
Census may be taken for a variety of reasons.
David was punished not for taking a census but for disobeying God.