我在分组以下语句时遇到错误。这是我的代码
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
答案 0 :(得分:2)
您发布的查询中有3个问题:
DATENAME
。 From
子句中定义的内部查询实际上是'Derived Table'
时。它们的存在范围是外部查询。外部查询完成后,派生表就会消失。因此,我们需要指定在括号内定义派生表的查询,后跟AS子句和派生表名。 (下面我将其写为'T'
)写为:
--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之间找到了空格,请参阅上面的更正。