取平均值时,关键字“GROUP”IN SQL附近的语法不正确

时间:2014-02-28 04:58:21

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

我在分组以下语句时遇到错误。这是我的代码

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),
         DATENA ME(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

3 个答案:

答案 0 :(得分:2)

您发布的查询中有3个问题:

  1. 正如大家所指出的那样,正确的标识符DATENA ME为DATENAME
  2. 当外部查询的From子句中定义的内部查询实际上是'Derived Table'时。它们的存在范围是外部查询。外部查询完成后,派生表就会消失。因此,我们需要指定在括号内定义派生表的查询,后跟AS子句和派生表名。 (下面我将其写为'T'
  3. 现在我们在外部查询中引用派生表中的列,只能访问其中定义的列。 (所以我已经更正了外部查询列)
  4. 写为:

    --DECLARE @avg_volume int
    SELECT ISNULL(AVG(Total_Volume),0) as average_volume,
             station_id,
             MONTH,
             YEAR,
             weekday
    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)) AS T
    WHERE WEEKDAY = 'Thursday' AND MONTH=2
    GROUP BY station_id,
             MONTH,
             YEAR,
             weekday
    ORDER BY YEAR,
             MONTH
    

    SQL FIDDLE: http://sqlfiddle.com/#!3/6217d/10

答案 1 :(得分:1)

您的代码中有一个空格DATENAME

DATENA ME(weekday,date_time) 

写为

DATENAME(weekday,date_time) 

------------------------另一点--------------------- -----

您必须为子查询使用名称,例如

SELECT t1.yourColumn
(
select yourColumn
FROM tableABC
) t1 <you not assign name here to newly subquery created table e.g t1>

还使用

等列别名
SELECT t1.aliasName
(
select yourColumn as [aliasName] 
FROM tableABC
) t1
ORDER BY t1.aliasName

答案 2 :(得分:0)

为内部查询指定别名,然后在语句外部表组中使用该别名

(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)
) AS MYJUNCTIONTABLE 

此外,我在复制代码时在DATE NA ME之间找到了空格,请参阅上面的更正。