Added function to build time inserted query