I have a table (let’s call it tableA) that looks something like this:
id | dateA | dateB
----------|------------|-------------
1 | 2011-01-01 | 2010-05-01
1 | 2011-05-01 | 2010-06-12
2 | 2011-01-11 | 2010-01-31
2 | 2011-01-31 | 2010-02-01
3 | 2011-05-11 | 2010-08-02
3 | 2011-08-02 | 2010-09-10
My goal is to group rows by id, and get minimum of dateA and maximum of dateB
and get a table that looks something like this:
id | min(dateA) | max(dateB)
----------|------------|-------------
1 | 2011-01-01 | 2010-06-12
2 | 2011-01-11 | 2010-02-01
3 | 2011-05-11 | 2010-09-11
Right now, I’m using LEFT join approach:
SELECT
id,
tableB.dateA,
tableC.dateB
FROM tableA as a
LEFT JOIN (
SELECT id, min(dateA)
FROM tableA
GROUP BY id
)tableB ON a.id = tableB.id
LEFT JOIN (
SELECT id, max(dateB)
FROM tableA
GROUP BY id
)tableC ON a.id = tableC.id
However, my approach is way too slow. I have pretty big table, and it takes about 7seconds to get the desired outcome.
Could anyone suggest me a good optimizing technique to apply to my situation?
Thank you.
J
What’s wrong with a simple GROUP BY using one pass through the table and no self-joins?