Fixes bug in SQL for discrepancy