Constructing a calculated query