In TNG, Riker is the first officer and has the rank of Commander. Lt. Commander Data is the 2nd officer, yet Crusher outranks Data. Why is it that he's the 2nd officer and she isn't?

Where does she fit into the command hierarchy?