2016-08-24 87 views
0

我有一個查詢我運行得是我,我想幾乎什麼....排序由一列匹配值

下面是運行該什麼時候我得到現在:

select users.username as spoc, iata.code as site, 
     completed.request as SR, truncate(count(tasks.task) *100/24/1,2) as percentage 
from completed 
join users on users.id = completed.name 
join tasks on tasks.id = completed.task 
join iata on iata.id = completed.code 
group by iata.code 
order by users.username; 

+-------------------+----------------+ 
| spoc | site | SR | percentage | 
+-------------------+------+------+--+ 
| carmen | PVD | 1110 |  58.33 | 
| everton | BUD | 1114 |  120.83 | 
| everton | BUS | 1112 |  110.33 | 
| everton | BUS | 1112 |  110.33 | 
| everton | BUS | 1113 |  110.33 | 
| everton | BUS | 1112 |  110.33 | 
| everton | BUS | 1112 |  110.33 | 
| everton | BUD | 1114 |  120.83 | 
| everton | BUD | 1115 |  120.83 | 
| everton | BUD | 1115 |  45.83 | 

其中一些值超過100%的原因是因爲它們在不同的SR下重複執行任務,並且在COUNT中計算它們全部。

我在查詢中查找的是COUNT任務通過獨特的SR ....這種方式他們不應該超過100%。我希望這是有道理的。

+0

什麼是'24'所有列使用GROUP BY?您將其作爲百分比計算的一部分。 – Ibu

+0

'count(distinct tasks.task)' – Fabricator

回答

0

你建議立即進行刪除不聚集功能

select users.username as spoc, iata.code as site, 
    completed.request as SR, truncate(count(tasks.task) *100/24/1,2) as percentage 
from completed 
join users on users.id = completed.name 
join tasks on tasks.id = completed.task 
join iata on iata.id = completed.code 
group by users.username, iata.code, completed.request 
order by users.username; 
+0

24是任務的數量......我除以得到百分比。 – Xyz

+0

感謝scaisedge,我會嘗試。 – Xyz