我在分组声明时遇到错误。这是我的代码
DECLARE @avg_volume INT
SELECT @avg_volume = ISNULL(AVG(Total_Volume), 0)
FROM ( SELECT station_id ,
DATEPART(Year, date_time) AS YEAR ,
DATEPART(month, date_time) AS MONTH ,
CONVERT(DATE, date_time) AS DATE ,
DATENAME(weekday, date_time) AS weekday ,
SUM(volume) AS Total_volume
FROM rvtcs_data_aggregated_hourly
GROUP BY station_id ,
CONVERT(DATE, date_time) ,
DATEPART(month, date_time) ,
DATEPART(Year, date_time) ,
DATENAME(weekday, date_time)
)
GROUP BY station_id ,
CONVERT(DATE, date_time) ,
DATEPART(month, date_time) ,
DATEPART(Year, date_time) ,
DATENAME(weekday, date_time)
ORDER BY DATEPART(Year, date_time) ,
DATEPART(month, date_time) ,
CONVERT(DATE, date_time)
SELECT @avg_volume
我要做的是,通过一组条件(内部查询)从卷组中获取总和,这将得到结果
station_id YEAR MONTH DATE weekday Total_volume
7 2013 2 2013-02-21 Thursday 192
7 2013 2 2013-02-27 Wednesday 2699
7 2013 2 2013-02-28 Thursday 196
2 2013 3 2013-03-07 Thursday 192
7 2013 3 2013-03-07 Thursday 192
现在我想取平均值。任何帮助都会有所帮助。
我的表主表
station_id date_time volume
7 2013-02-21 00:00:00.000 96
7 2013-02-21 01:00:00.000 96
7 2013-02-27 00:00:00.000 356
7 2013-02-27 00:00:00.000 410
7 2013-02-27 00:00:00.000 471
7 2013-02-27 00:00:00.000 530
7 2013-02-27 00:00:00.000 338
7 2013-02-27 00:00:00.000 211
7 2013-02-27 00:00:00.000 159
7 2013-02-27 00:00:00.000 128
7 2013-02-27 00:00:00.000 96
7 2013-02-28 00:00:00.000 96
7 2013-02-28 01:00:00.000 100
7 2013-03-07 00:00:00.000 96
2 2013-03-07 00:00:00.000 96
2 2013-03-07 01:00:00.000 96
7 2013-03-07 01:00:00.000 96
期望的输出:
station id year month weekday average_volume
7 2013 2 Thursday 194
答案 0 :(得分:2)
您需要命名您的子查询:
DECLARE @avg_volume INT
SELECT @avg_volume = ISNULL(AVG(Total_Volume), 0)
FROM ( SELECT station_id ,
DATEPART(Year, date_time) AS YEAR ,
DATEPART(month, date_time) AS MONTH ,
CONVERT(DATE, date_time) AS DATE ,
DATENAME(weekday, date_time) AS weekday ,
SUM(volume) AS Total_volume
FROM rvtcs_data_aggregated_hourly
GROUP BY station_id ,
CONVERT(DATE, date_time) ,
DATEPART(month, date_time) ,
DATEPART(Year, date_time) ,
DATENAME(weekday, date_time)
) AnyNameYouLikeButYouHaveToGiveOne --<-- Here
GROUP BY station_id ,
CONVERT(DATE, date_time) ,
DATEPART(month, date_time) ,
DATEPART(Year, date_time) ,
DATENAME(weekday, date_time)
ORDER BY DATEPART(Year, date_time) ,
DATEPART(month, date_time) ,
CONVERT(DATE, date_time)
SELECT @avg_volume
答案 1 :(得分:1)
您的查询中几乎没有基本问题。您总结并找出基于月份日ETC的平均值。我没有看到任何过滤器,一年或一个月或站ID。所以我假设你的最终选择查询中会有多行,而你正在选择一个变量。所以你可能只得到变量中第一行/最后一行的值(当然取决于排序)。你必须在这里对查询做一些修改。
此查询是否为您提供了正确的输出?
SELECT ISNULL(AVG(Total_volume), 0)
FROM ( SELECT station_id ,
DATEPART(year, date_time) AS YEAR ,
DATEPART(month, date_time) AS MONTH ,
CONVERT(DATE, date_time) AS DATE ,
DATENAME(weekday, date_time) AS weekday ,
SUM(volume) AS Total_volume
FROM rvtcs_data_aggregated_hourly
GROUP BY station_id ,
CONVERT(DATE, date_time) ,
DATEPART(month, date_time) ,
DATEPART(year, date_time) ,
DATENAME(weekday, date_time)
ORDER BY DATEPART(year, date_time) ,
DATEPART(month, date_time) ,
CONVERT(DATE, date_time)
) T;
答案 2 :(得分:1)
根据您的目的,您不需要进行第二次显式聚合或order by
。只取平均值:
DECLARE @avg_volume INT
SELECT @avg_volume = ISNULL(AVG(Total_Volume), 0)
FROM ( SELECT station_id ,
DATEPART(Year, date_time) AS "YEAR" ,
DATEPART(month, date_time) AS "MONTH" ,
CONVERT(DATE, date_time) AS "date" ,
DATENAME(weekday, date_time) AS weekday ,
SUM(volume) AS Total_volume
FROM rvtcs_data_aggregated_hourly
GROUP BY station_id ,
CONVERT(DATE, date_time) ,
DATEPART(month, date_time) ,
DATEPART(Year, date_time) ,
DATENAME(weekday, date_time)
) t;
您的具体错误是因为您在子查询中没有别名(好吧,关键字group
不算作别名)。
你的一些专栏命名为关键字,所以我引用了这些。这些单词的列表是here。
实际上,在编写这样的查询时,您不需要在select
中包含所有列,所以您也可以这样做:
DECLARE @avg_volume INT
SELECT @avg_volume = ISNULL(AVG(Total_Volume), 0)
FROM ( SELECT SUM(volume) AS Total_volume
FROM rvtcs_data_aggregated_hourly
GROUP BY station_id ,
CONVERT(DATE, date_time) ,
DATEPART(month, date_time) ,
DATEPART(Year, date_time) ,
DATENAME(weekday, date_time)
) t;