strSql = "SELECT DISTINCT T_AUTHOR FROM " & strActivePrefix & "TOPICS"SELECT DISTINCT T_AUTHOR,[OTHER_FIELDS]
FROM FORUM_TOPICS
WHERE [CLAUSES]
GROUP BY T_AUTHOR
ORDER BY [SORT_FIELD]
LIMIT [X]
SELECT DISTINCT T_AUTHOR, T_SUBJECT, T_LAST_POST FROM FORUM_TOPICS
GROUP BY T_AUTHOR
ORDER BY T_LAST_POST DESC
LIMIT 20
Originally posted by HuwRI don't think that applies to a query that does not include aggregation functions, Huw.
in MSSQL you can't groupby T_AUTHOR if the select statement contains other columns, it will just error as the other columns are not in the aggregation function or the groupby.
Originally posted by HuwRI don't think that applies to a query that does not include aggregation functions, Huw. it applies to any query with more than one column in the select statement, they must either be in the group by or in an aggregate function as in my example above.
in MSSQL you can't groupby T_AUTHOR if the select statement contains other columns, it will just error as the other columns are not in the aggregation function or the groupby.
Originally posted by HuwRYou are right about this. Your example is not the best one to counter my earlier objection, since you do have an aggregation function in there, but I confirmed it anyway.
it applies to any query with more than one column in the select statement, they must either be in the group by or in an aggregate function as in my example above.
Originally posted by HuwRYes, I had reached the same conclusion.
you can do it without the group by, but it still requires an agregate function, but it is just not possible to get all he info required in a simple or complex query because you can't return all the info you need like topicid etc inthe same query
CREATE FUNCTION GetLastTopicsOneByPoster()
RETURNS @topics TABLE
(
MEMBER_ID int PRIMARY KEY,
TOPIC_ID int
)
As
BEGIN
INSERT INTO @topics
SELECT T_AUTHOR, (SELECT TOPIC_ID FROM FORUM_TOPICS T2 WHERE T2.T_AUTHOR=T1.T_AUTHOR AND T2.T_LAST_POST=T1.LASTPOST) As T_ID
FROM (
SELECT TOP (10) T_AUTHOR, MAX(T_LAST_POST) AS LastPost
FROM FORUM_TOPICS
GROUP BY T_AUTHOR
ORDER BY LastPost DESC
) As T1
RETURN
END
SELECT T.* FROM FORUM_TOPICS T INNER JOIN GetLastTopicsOneByPoster() G On T.TOPIC_ID=G.TOPIC_ID
Originally posted by HuwRYeah, just in memory...
well, you are infact creating a temp tablethat is what @topics is
Originally posted by HuwRGo away, mine is much neater
code posted above to do the same with just an inline query, no functions needed
Originally posted by HuwROh yeah?
code posted above to do the same with just an inline query, no functions needed
SELECT T.* FROM FORUM_TOPICS T INNER JOIN
(SELECT T_AUTHOR, (SELECT TOPIC_ID FROM FORUM_TOPICS T2 WHERE T2.T_AUTHOR=T1.T_AUTHOR AND T2.T_LAST_POST=T1.LASTPOST) As T_ID
FROM (
SELECT TOP (10) T_AUTHOR, MAX(T_LAST_POST) AS LastPost
FROM FORUM_TOPICS
GROUP BY T_AUTHOR
ORDER BY LastPost DESC
)T1
) As MyT ON MyT.T_ID=T.TOPIC_ID
ORDER BY T_LAST_POST DESC
Originally posted by HuwRIt's not simple ?!!
well, to be honest four nested selects isn't exactly simple![]()