2016-11-03 67 views
0

我有一個表包含卡車ID &卡車ID可以重複(旅行)的里程。 我想要一個查詢,顯示最大(里程)-Min(里程),但只有當卡車ID> 1的計數。對不起,我仍然是這個世界上的初學者。使用計數條件

+1

那你試試? – rbr94

+0

我們的答案對您有幫助嗎? – rbr94

回答

2
Select Max(mileage)-Min(Mileage) as diff, TruckID 
from tableTruck 
group by truckid 
having count(truckid) > 1 

使用此查詢

2

嘗試使用GROUP BY ... HAVING:在HAVING條款您發現這裏

SELECT MAX(mileage)-MIN(Mileage), Truck_Id 
FROM YourTable 
GROUP BY Truck_Id 
HAVING COUNT(Truck_id) > 1 

一個小教程,範例:http://www.w3schools.com/sql/sql_having.asp