rewritten sql integration