I am running a query in SQL Server 2005. The query needs to have a column that consists of the same number the whole way down. The number is the avg of another row in the query. Here is what I have, maybe it will help make sense of what I am trying. This data has ‘color’ values and the ‘DataExtraLineValue’ the same the whole way down because the software that reads this query is spitting out a chart. Basically, why cant I run AVG(TagValueInteger) AS DataExtraLineValue
SELECT
RecordedDateTime AS DataGroup,
TagValueInteger AS DataBar,
TagValueInteger AS DataLine,
'Green' AS DataColor,
'Avg' AS DataExtraLineLabel,
AVG(TagValueInteger) AS DataExtraLineValue,
'Blue' AS DataExtraLineColor
FROM tTagHistory
WHERE
(TagHistoryDefinitionID = 2) AND
(IsQualityGood = 1) AND
(DeltaValueInteger <> 0) AND
(TagValue > 4) AND
(TagValue < 60) AND
(RecordedDateTime > (GetDate()-2))
ORDER BY RecordedDateTime DESC
You can’t do that because there’s no grouping. Without grouping, your
AVGis going to be the value for each row. If you want the average for ALL values of that field for the whole table, you can make it a subquery: