Why did God order a census in Numbers 1, especially considering the fact that God sent judgment when King David later ordered one?
It is helpful to notice, first, that the act of taking a census was not sinful in itself, and second, the numbering of the children of Israel referred to in Numbers 1 was a divinely given command. Scripture records several numberings taken in the life of Israel (e.g., Exo 30:11-16; Num 1; Num 26; 2Ch 2:17-18; Ezr 2; Neh 7). The law given in Exodus 30:11-16 concerning the ransom was connected with numbering the children of Israel – whenever that occasion arose. No less than five times does a reference to “numbering” occur in verses 12 to 14. From this we discover that taking “a census” (NASB) was as much a part of the law as was the commandment for each one to give half a shekel as “a ransom for his soul unto the Lord” (vv12,13,14). In some instances (e.g., 2Ch 2), the numbering of people was not divinely commanded but sanctioned without receiving judgement from God.
In Numbers 1, Moses and Aaron were commanded to sum the children of Israel “every male … from twenty years old and upward, all that are able to go forth to war in Israel” (vv2,3). The same is given to Moses and Eleazar at the close of the 38-year wilderness journey (Num 26:2). Literally, the children of Israel were the Lord’s host – united, redeemed, preserved, numbered, ordered and prepared for warfare. As “the Lord is a man of war: the Lord is His name” (Exo 15:3), He alone has the prerogative to select and number the hosts of heaven and earth to accomplish His divine will. Spiritually, the lesson is clear: we are His redeemed servants in a wilderness wide, engaged in warfare (Eph 6:10-18); mature men in the sense of “a perfect [teleios, ‘mature’] man, unto the measure of the stature of the fulness of Christ” (Eph 4:13); able to “endure hardness, as a good soldier of Jesus Christ” (2Ti 2:3); to go forth that we “may please him who hath chosen [us] to be a soldier” (v4).
In David’s case, there was no command from Jehovah to prepare for battle. Rather, we read that “the anger of the Lord was kindled against Israel, and he moved David against them to say, Go, number Israel and Judah” (2Sa 24:1; cf. Pro 21:1). Drawing from the accounts in 2 Samuel 24 and 1 Chronicles 21, we learn that David fell prey to the temptation of pride and vain glory; his motivation was to discover the military strength of his kingdom. His motivation was not God’s glory, but his own (Pro 16:5). His numbering in this regard was a sin in the sight of the Lord, and he owned it as such (1Ch 21:6-8). Even carnal Joab saw the futility of David’s actions. We also learn that the nation had transgressed and departed from the Lord, and for this they were afflicted with judgement (2Sa 24:15). According to divine grace, provision is made, through David’s building an altar and offering burnt offerings and peace offerings, for David’s restoration and the staying of God’s wrath against the nation.
 All remaining Scripture quotations are from the KJV.