Updated pivot query