以下是我创建表格的方法:
CREATE TABLE #tmp
(
[Counter] int
,Per date not null
,Cam float
,CamMeg float
,Hfx float
,HfxMet float
,TorMetric float
)
以下是我稍后在脚本中调用该表的方法:
SELECT
((ROW_NUMBER() over(order by Per desc)-1)/@Avg)+1 as [Counter], Per,
Cam,
AVG(CamMetric) as CamMet,
HfxMe,
FROM #tmp
GROUP BY [counter] ;
DROP TABLE #tmp
以下是我得到的错误:
Msg 8120,Level 16,State 1,Line 175 列'#tmp.Per'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
Msg 8120,Level 16,State 1,Line 175 列'#tmp.Per'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
我做错了什么?
数据如下所示
counter --- per ---Cam --- HfxMet ---......
1 2012-02-09 3 16
1 2012-02-24 4 12
1 2012-03-04 2 15
2 2012-03-15 1 18
2 2012-03-30 6 20
2 2012-04-07 10 6
3 2012-04-28 8 3
现在我想再添加两个名为CamMetricAvg和HfxMetric的列,它们将查看所有1的计数器,然后分别获取CamMetric和HfxMetric值并给出平均值并将其放在每个列上如下所示:
counter --- per ---Cam --- CamMt ---
1 2012-02-09 3 3
1 2012-02-24 4 3
1 2012-03-04 2 3
2 2012-03-15 1 5.6
2 2012-03-30 6 5.6
2 2012-04-07 10 5.6
3 2012-04-28 8 8
答案 0 :(得分:1)
SELECT [Counter], Period,
CamMetric,
AvgCamMetric = AVG(CamMetric) OVER(PARTITION BY Counter),
HfxMetric,
AvgHfxMetric = AVG(HfxMetric) OVER(PARTITION BY Counter)
... repeat for other metrics ...
FROM #tmpTransHrsData
GROUP BY [Counter], Period, CamMetric, HfxMetric;