How to group by two rows in a column

Left join location table on data to get all locations regardless if there is match in data table, group by location id and location, and I assume you want to get the 1st name (min()):

select l.id, l.location, min(d.name) as name
from location l
left join data d on l.location=d.location
group by l.id, l.location

Alternatively, if you want to get for a data item if it has offshore and onshore record:

select d.id, l.location, d2.name
from location l
join data d
left join data d2 on l.location=d2.location
where d.id=1

Leave a Comment