how to account different table sql by time decision