I have a table called "lane" with the following properties.
CREATE TABLE `lane` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`origRegion` varchar(45) NOT NULL,
`origState` char(2) NOT NULL,
`destRegion` varchar(45) NOT NULL,
`destState` char(2) NOT NULL
PRIMARY KEY (`id`)
)
There are duplicate rows in this table of the following columns: origState, origRegion, destState, destRegion. I'd like to be able to select all rows JOINED to what the min(id) is of the first occurance.
For example, with data:
1 ALL MA ALL OH 2 ALL MA ALL OH 3 ALL MA ALL OH
and a SQL similar to this (which misses all the duplicate rows):
select l.*, l2.count, l2.minId from tmpLane l
JOIN (SELECT id, min(ID) as minId from tmpLane
GROUP BY origRegion, origState, destRegion, destState) l2 on l.id = l2.id;
Result (note the count and minId at the end):
1 ALL MA ALL OH 3 1 2 ALL MA ALL OH 3 1 3 ALL MA ALL OH 3 1
Note, that the query used above is an adaptation of the solution here (which doesn't work in this situation)