A few weeks ago I asked a question about eliminating duplicate records in a SQL INNER JOIN. The code I ended up using is similar to the following answer:
SELECT ROW_NUMBER() OVER(ORDER BY " + orderField + @") AS RowNum,
mt.ID AS mt_ID,
mt.title AS mt_title,
[...]
MAX(st.title) AS st_title,
-- Other aggregates (MAX, MIN, AVERAGE, ...) for all other columns
-- from sttable, whatever is appropriate.
[...]
FROM mttable AS mt
INNER JOIN sttable AS st on mt.ID =st.ID
WHERE st.field <> 0 AND mt.title = @title
GROUP BY mt.ID,
mt.title
-- Group by everything else from mttable.
This works well enough at eliminating duplicates, but the problem I have now is that I want to perform queries on sttable (the table that is not grouped), and the GROUP BY eliminates this data. For example, I want to be able to run the query WHERE st.title = '...'
Is there any way I can achieve this? Thanks.