[RESOLVED] Sql Problem

Hi All,

I currently have the following SQL Statement:

select c.Id, c.name, c.active, a.name as attribute
from component as c join attribute as a on c.attributeid = a.id
order by c.name asc

this gives me data in the form:

183 Communal Enclosure 1 Cleaning
150 Composite 1 Doors Generally
21 Composite 1 Windows
33 Composite 1 Doors Generally
36 Door Frame 1 Doors Generally
46 Door Frame 1 Doors Communal
154 Door Frame 1 Doors Generally
The problem is that I have duplicates of columns c.name and a.name with different Id's

I want to list distinct based on these 2 columns and furthermore want only the row with the highest id on duplicates returned. How should the Select statement be changed? It can be a stored proc.

Thanks.
[961 byte] By [Bill Crawley] at [2007-11-20 11:26:44]
# 1 Re: [RESOLVED] Sql Problem
I haven't tried this SQL, but it should work for what you want:
SELECT MAX(c.Id) AS Id, c.name, c.active, a.name AS attribute
FROM component c
INNER JOIN attribute a ON c.attributeid = a.id
GROUP BY c.name, c.active, a.name
ORDER BY c.name
andreasblixt at 2007-11-9 13:45:40 >
# 2 Re: [RESOLVED] Sql Problem
Thanks for your help.

This seems to work:

select c.Id, c.[name], c.active, a.[name] as attribute
from
component as c
join
attribute as a on c.attributeid = a.id
inner join
(select max(c.id) as cid ,c.[name],a.[name] e from component as c
join attribute as a on c.attributeid = a.id group by c.name, a.name) a1 ON a1.cid = c.id
order by c.name asc
Bill Crawley at 2007-11-9 13:46:40 >