Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GROUP BY and DISTINCT

Status
Not open for further replies.

Echilon

Programmer
Feb 22, 2007
54
GB
I have a few tables and I need to select multiple fields from each. This query works but it doesn't class rows as unique, since if multiple translationids exist, there are multiple rows with the same translationid. I need a single row for each translationid.

SELECT DISTINCT elements.translationid, translations.language, translations.translation, translationcomments.comment
FROM elements
INNER JOIN translations ON translations.translationid = elements.translationid
LEFT JOIN translationcomments ON translationcomments.translationid = translations.translationid

ORDER BY elements.id

If I add a group by clause for "translationid", for a reason I don't understand only a quarter or so of the results are returned. Is there a way I can select just the unique translationids?
 
Is there a way I can select just the unique translationids?
certainly
Code:
SELECT DISTINCT translationid FROM elements
:)


okay, sorry, i know you wanted something more

the point here is that if you want one row per translationid, along with other stuff in the SELECT clause, then all the other stuff in the SELECT clause has to be aggregate functions
Code:
SELECT [red]elements.translationid[/red]
     , [blue]MAX(translations.language)[/blue]
     , [blue]MIN(translations.translation)[/blue]
     , [blue]MAX(translationcomments.comment)[/blue]
  FROM elements 
INNER 
  JOIN translations 
    ON translations.translationid = elements.translationid 
LEFT OUTER
  JOIN translationcomments 
    ON translationcomments.translationid = translations.translationid 
GROUP 
    BY [red]elements.translationid[/red]
but that probably isn't going to help you much either

perhaps you could give some more information about what other stuff you want along with the unique translationids?

r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top