-2

我有一张如下表

carrier   duration   date
a         10         05-09-2013 
a         0          04-09-2013 
b         1          05-09-2013 
b         3          04-09-2013  
c         2          05-09-2013 
c         3          04-09-2013

我想根据运营商和总持续时间计算平均值

结果应该是:

carrier a = avg is 10
carrier b = avg is 2
carrier c = ave is 2.5

它应该只计算非零值的总持续时间,因此对于承运人 A,因为它在 04-09-2013 日期有任何活动,那么平均值应该是 10/1,即总持续时间/非零持续时间的日期数)

4

3 回答 3

1
select                  
  carrier,           -- carrier column in result set
  avg(duration)      -- average duration column in result set
from tablename       -- specify what table to get results from
where duration>0     -- condition excluding table rows where duration=0
group by carrier;    -- All rows with the same 'carrier' value are grouped 
                     -- together into one row in the result set. This works with 
                     -- the aggregate function avg();
于 2013-09-16T09:05:14.250 回答
0
select carrier,AVG(duration) as average_duration
from tablename
where duration != 0
group by carrier
于 2013-09-16T08:58:01.637 回答
0
select carrier, avg(duration) 
from mytable
where duration > 0
group by carrier
于 2013-09-16T08:54:52.133 回答