2011-12-07 28 views
0

我們根據預計完成的工作量除以付給的小時數計算獎金。我們系統中有一個人沒有被我們支付,我想把他從報告中刪除。如何在不刪除符合聯接條件的所有條目的情況下限制選擇?

在此查詢我得到的所有跟蹤的時間每批該用戶通過跟蹤在該批次的總時間除以減去[名]。

批次的任務包含estimated_nonrecurring和estimated_recurring。 批次有batch_logs,其中有time_elapsed,這是該批次實際工作的時間。

發生的事情不是選擇所有批次,然後獲取跟蹤該用戶的所有時間與跟蹤的總時間相比未被[名稱]跟蹤,而是忽略每個已批次[姓名]作爲條目。

這是我對比率的選擇聲明。

SELECT user_time_by_batch.batch_id, userid, user_time_by_batch/total_time_by_batch as ratio FROM 
( 
SELECT SUM(time_elapsed) user_time_by_batch, userid, batch_id 
FROM batch_log 
inner join batches on batch_log.batch_id = batches.id 
Where start_time between (?) and (?) 
    and end_time is not null and time_elapsed BETWEEN \"00:00:00\" AND \"10:00:00\" 
    and batch_id not in (\"-1\", \"-2\", \"-3\", \"-4\", \"-5\", \"-6\", \"-7\", \"-8\", \"-9\", \"-10\", \"-11\", \"-12\", \"-13\", \"-14\", \"-15\", \"-16\", \"-17\") 
    and batches.operation_id = ? 
    and batch_log.userid = ? 
GROUP BY batch_id, userid 
) user_time_by_batch 
INNER JOIN 
( 
SELECT SUM(time_elapsed) total_time_by_batch, batch_id 
FROM batch_log 
inner join batches on batch_log.batch_id = batches.id 
Where start_time between (?) and (?) 
    and end_time is not null and time_elapsed BETWEEN \"00:00:00\" AND \"10:00:00\" 
    and batch_id not in (\"-1\", \"-2\", \"-3\", \"-4\", \"-5\", \"-6\", \"-7\", \"-8\", \"-9\", \"-10\", \"-11\", \"-12\", \"-13\", \"-14\", \"-15\", \"-16\", \"-17\") 
    and batches.operation_id = ? 
    and batch_log.userid != 'name' 
GROUP BY batch_id 
) total_time_by_batch 
ON user_time_by_batch.batch_id = total_time_by_batch.batch_id ; 
+1

你說「這是忽略所有的已經smacpherson作爲一個條目批」。我認爲這正是你要說的。我的猜測是,你需要按用戶和批次(不是批量單獨)在這些總和中排除McPherson,然後將這些總和按批計算以獲得批次總計。 – mikeY

+0

是的,我只是試圖讓他脫離批處理:如果bob工作6小時,mac工作3,那麼鮑勃應該有1.0作爲比例,而不是6.66666 – davidahines

+1

我不知道,但我認爲你是什麼現在做的是說「總結這一點,但不要選擇與Mac的任何批次」。我想你想要做的就是彙總所有不是mac的用戶,然後總結他們的時間來創建你的批次總數作比較。然後鮑勃是1.0。但我可以離開基地。 – mikeY

回答

1

有一件事我會考慮嘗試正朝着batch_log.userid!=到JOIN語句,而不是在WHERE子句中「smacpherson」。我不確定這是多麼好的實踐,但我已經成功地通過這種方式消除了奇怪的數據問題。

所以:

INNER JOIN 
( 
SELECT SUM(time_elapsed) total_time_by_batch, batch_id 
FROM batch_log 
inner join batches on batch_log.batch_id = batches.id and batch_log.userid != 'smacpherson' 
Where start_time between (?) and (?) 
    and end_time is not null and time_elapsed BETWEEN \"00:00:00\" AND \"10:00:00\" 
    and batch_id not in (\"-1\", \"-2\", \"-3\", \"-4\", \"-5\", \"-6\", \"-7\", \"-8\", \"-9\", \"-10\", \"-11\", \"-12\", \"-13\", \"-14\", \"-15\", \"-16\", \"-17\") 
    and batches.operation_id = ? 
GROUP BY batch_id 
) total_time_by_batch 
+0

會試試這個。看起來可能是我需要的。 – davidahines

+1

我想提出建議,但我不知道它是否可行。我沒有將mySQL和Access扔在一個我喜歡的測試連接上。有興趣看看你有沒有成功。 – mikeY

+0

嗯..我不明白加入選擇。 – davidahines

相關問題