SQL选择总和/平均所有行

时间:2015-05-26 12:10:01

标签: sql-server select sum average

我有这个代码从表中选择行,但我需要让他返回所有行的总和或所有行的平均值。我尝试将代码放在其他选择中,例如在sum(select)中选择,但它不起作用,任何想法如何使其工作?很抱歉没有多少信息。现在它打印25行。我需要让它返回1行,就像sum(ontime)和avg(actual)。我的意思是将所有25行的ontime列和avg实际列相加。

 SELECT    AVG(CASE WHEN a.ontime = 'on time' THEN 100 ELSE 0 END)*COUNT(a.country) AS ontime
    ,AVG(CASE WHEN a.accuracy = 'accurate' THEN 100 ELSE 0 END) AS actual
    ,COUNT(a.country) AS task_count

FROM 
    bstplanning.dbo.sc_data a
    INNER JOIN bstplanning.dbo.users 
    ON a.user_id = bstplanning.dbo.users.user_id    

WHERE
    a.date >= '2015-04-01 00:00:00.000' AND a.date <= '2015-04-30 00:00:00.000' AND a.actual > 0 AND
    ((a.ontime='on time' OR a.ontime = 'late') OR (a.accuracy='accurate' OR a.accuracy = 'error'))
     and bstplanning.dbo.users.split='Rep'
     and bstplanning.dbo.users.user_surname='user'
GROUP BY
     a.country
    ,a.client
    ,a.task_group
    ,frequency
    ,a.track

1 个答案:

答案 0 :(得分:1)

你可以像外部查询和你的查询一样编写内部查询..

WebView