Subject Re: DISTINCT and LEFT JOIN (was: sort record size of 69708 bytes is too big)
Author Svein Erling Tysvær
Here's another suggestion that doesn't explain your problem, but that
probably helps you solve it by doing things slightly differently:

SELECT P.ID, P.USERID, U.USERNAME, P.MSG, P.POSTED,
P.LAST_EDITED, P.LAST_EDITED_BY, U1.USERNAME, P.IPADDRESS, P.TOPICID,
U.SHOW_SIG, U.SIGNATURE
FROM DNFBB_POSTS P
LEFT JOIN DNFBB_USERS U ON (U.ID = P.USERID)
LEFT JOIN DNFBB_USERS U1 ON (U1.ID = P.LAST_EDITED_BY)
WHERE (P.TOPICID = 1) AND
EXISTS(SELECT *
FROM DNFBB_TOPICS T
JOIN DNFBB_FORUMS F ON (F.ID = T.FORUMID)
JOIN DNFBB_USERGROUPSTOFORUM UGUF ON (UGUF.FORUM_ID = F.ID)
JOIN DNFBB_USERTOUSERGROUPS UTUG
ON (UTUG.USERGROUP_ID = UGUF.USERGROUP_ID)
WHERE T.ID = P.TOPICID AND UTUG.USER_ID = 1);

i.e. no DISTINCT, but move the part that I presume made DISTINCT
necessary - even though it isn't in any output column - into a simple
EXISTS part.

Though this only helps if my assumptions that the selected column with
this statement is DISTINCT by nature. And, I guess I'm not the only
person still curious why your initial statement failed.

HTH,
Set

--- In firebird-support@yahoogroups.com, "Si Carter" wrote:
> > If there are no NULLS or users that aren't in P.USERID or P.
> > LAST_EDITED_BY, you may even avoid LEFT JOINs altogether, and if
> > not an alternative would be to change to subselects for those two
> > USERNAME-fields.
>
> Your right there, I missed the boat on that one. I will change the
> sql and test it.
>
> FWIW, the distinct still raised the existing exception though :-(