从查询结果中查找数据总和

时间:2014-02-20 05:35:45

标签: sql sql-server sql-server-2008

如何找到以下查询的结果总和

 select DATEPART(month,date) as month ,sum(volume) as Total_Volume
from    traffic_data_replica
where   DATENAME(weekday,date)='sunday' and DATENAME(year,date)=2013 
group by DATEPART(month,date)
order by DATEPART(month,date);

简而言之,上述查询返回的结果总和

2 个答案:

答案 0 :(得分:2)

如果您想要在当前查询中的行上收到所有Total_Volume的总和,则可以使用:

SELECT SUM(Total_Volume) 
FROM (
    select DATEPART(month,date) as month ,
    sum(volume) as Total_Volume 
    from traffic_data_replica 
    where 
    DATENAME(weekday,date)='sunday' 
    and DATENAME(year,date)=2013 
    group by DATEPART(month,date) 
    order by DATEPART(month,date)
) AS Data

如果要在当前查询中的行上收到行数,可以使用:

SELECT COUNT(*) 
FROM (
    select DATEPART(month,date) as month ,
    sum(volume) as Total_Volume 
    from traffic_data_replica 
    where 
    DATENAME(weekday,date)='sunday' 
    and DATENAME(year,date)=2013 
    group by DATEPART(month,date) 
    order by DATEPART(month,date)
) AS Data

答案 1 :(得分:0)

尝试类似

的内容
  SELECT COUNT(*) FROM 
(
  select DATEPART(month,date) as month ,sum(volume) as Total_Volume
from    traffic_data_replica
where   DATENAME(weekday,date)='sunday' and DATENAME(year,date)=2013 
group by DATEPART(month,date)
order by DATEPART(month,date)
) AS subquery;