简体   繁体   English

SQL关键字“ GROUP”附近的语法不正确

[英]Incorrect syntax near the keyword 'GROUP' IN SQL

I am facing an error while grouping a statement. 我在对语句进行分组时遇到错误。 Here is my code 这是我的代码

 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

What I am trying to do is ,taking the sum from volume group by a set of conditions(Inner query), which will give result as 我想做的是,通过一组条件(内部查询)从卷组中获取总和,其结果为

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

now I want to take average of that. 现在我想取其平均值。 Any assistance will be helpful. 任何帮助都会有所帮助。

My Table primary table 我的表主表

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

Desired Output : 所需输出:

station id     year  month     weekday    average_volume
  7            2013     2      Thursday     194

You need to name your subquery: 您需要命名子查询:

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

There are few fundamental issues in your query. 您的查询中几乎没有基本问题。 You are summing up and finding average on basis of month year Date ETC. 您正在汇总,并且根据月份年份日期ETC求平均值。 I do not see any filters, for a year or month or an station id. 我没有看到任何过滤器,例如年份或月份或站点ID。 So I assume there will be more than one row in your final select query and you are selecting into a variable. 因此,我认为最终选择查询中将有多于一行,并且您正在选择一个变量。 So you may get only the value from the first/last row in the variable(Of course depending on the ordering) .You have to make some modification to the query here. 因此,您可能只能从变量的第一行/最后一行获取值(当然,取决于顺序)。您必须在此处对查询进行一些修改。

Is this query giving you correct output? 该查询是否为您提供正确的输出?

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; 

Based on what you want to do, you don't need to do a second explicit aggregation or order by . 根据您要执行的操作,您无需进行第二次显式聚合或order by Just take the average: 仅取平均值:

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;

Your specific error was because you had no alias on the subquery (well, the keyword group doesn't count as an alias). 您的特定错误是因为您在子查询上没有别名(嗯,关键字group不算作别名)。

Some of your column names a keywords, so I quoted those. 您的某些专栏为关键词命名,因此我引用了这些关键词。 The list of such words is here . 这些单词的清单在这里

Actually, when writing a query like this, you don't need to include all the columns in the select , so you could also do: 实际上,当编写这样的查询时,您不需要在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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM