显然,当使用GROUP BY子句,那些列不聚集函数应该是group by子句的一部分。 这里的问题是,我不能包含GROUP HTTPADDRESS&DATEENTERED列BY子句。 另外,我不知道的功能,这将使我所有最新的条目。
编辑:我使用的SQL服务器。 如果我是使用访问我会用最后一个函数。
SQL = "SELECT VISITORIP, HTTPADDRESS, DATEENTERED"
SQL = SQL & " FROM STATS"
SQL = SQL & " WHERE DATEENTERED BETWEEN '" & OnlineTime & "' AND '" & NOW() & "'"
SQL = SQL & " GROUP BY VISITORIP"
SQL = SQL & " ORDER BY DATEENTERED DESC"
Set objOnVisitors = objConn.Execute(SQL)
你必须自联接回:
WITH LASTVISIT AS (
SELECT VISITORIP, MAX(DATEENTERED) AS DATEENTERED
FROM STATS
WHERE DATEENTERED BETWEEN @STARTTIME AND @ENDTIME
GROUP BY VISITORIP
)
SELECT STATS.VISITORIP, STATS.HTTPADDRESS, STATS.DATEENTERED
FROM STATS
INNER JOIN LASTVISIT
ON LASTVISIT.VISITORIP = STATS.VISITORIP
AND LASTVISIT.DATEENTERED = STATS.DATEENTERED
ORDER BY STATS.DATEENTERED DESC
请注意,这个假设给定VISITORIP将在范围DATEENTERED独特的最大值。
文章来源: Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause