2014-02-20 78 views
-1

我怎樣才能找到結果的總和以下查詢查找和結果

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); 

總之,上述查詢

+1

你是什麼意思的結果的總和多少?你需要查詢返回的記錄數還是Total_Volume的總和? –

+0

什麼是查詢錯誤? – Amit

+0

上面的查詢將返回結果行,我想它的總和 – bibinmatthew

回答

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 
+0

感謝bro @abhishek – bibinmatthew

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;