2016-02-29 97 views
0

我是新來的sql,我試圖劃分兩個選擇語句,但沒有得到正確的答案。我越來越70,但答案應該是9.20% 我在這裏做錯了什麼?劃分兩個Select語句?

select count(clocked_in_at - starts_at) 
from master_shift 
where clocked_in_at is not null 
and clocked_in_at <> starts_at 
and date_part('minute', clocked_in_at - starts_at) > 0 
/
(select count(id) from master_shift where clocked_in_at is not null) 

starts_at     clocked_in_at 
"2015-10-15 18:00:00+00";"2015-10-15 18:15:00+00" 
"2015-10-20 17:00:00+00";"2015-10-20 17:00:00+00" 
"2015-10-21 20:30:00+00";"2015-10-21 20:30:00+00" 
"2015-10-22 20:00:00+00";"2015-10-22 20:00:00+00" 
"2015-10-23 17:00:00+00";"2015-10-23 17:00:00+00" 
"2015-10-23 20:30:00+00";"2015-10-23 20:30:00+00" 
"2015-10-24 18:30:00+00";"2015-10-24 18:30:00+00" 
"2015-10-26 17:45:00+00";"2015-10-26 17:45:00+00" 
"2015-10-26 20:30:00+00";"2015-10-26 20:30:00+00" 
"2015-10-27 15:54:00+00";"2015-10-27 15:54:00+00" 
+0

你能提供的樣本數據? –

+0

@DarshanMehta - 我已經添加了示例數據 – bumblebeez

回答

0

你可能想這樣的:

SELECT 100 * sum(extract(second from (clocked_in_at - starts_at)))/count(id) 
FROM master_shift 
WHERE clocked_in_at IS NOT NULL 
AND clocked_in_at <> starts_at 
AND date_part('minute', clocked_in_at - starts_at) > 0;
+0

或者只是'AVG(clocked_in_at - starts_at)'。 – Barmar

+0

@Barmar如果每一行都有一個'id'值,那麼是的。如果'id'可能爲空,則count(id)將小於行數。 – Patrick

+0

@Patrick我如何把它變成%的條款? – bumblebeez