I have simplified my problem as follows:
Imagine a table containing details of play performances:
The data might look like this
I have been trying to write SQL which will return a list of first performances, theatre of first performance and the number of performances of each play as follows:
My first effort
does not return the correct theatre (when done with the real table).
As a work around I am running a query to get a list of plays and then running a separate query for each play to find the MIN(when) and theatre. This takes a long time as there are thousands of plays!
Can anyone suggest how I can get the desired result with one SQL statement?
I am using MySQL v5.0.13
Andrew
Hampshire, UK
Imagine a table containing details of play performances:
Code:
CREATE TABLE performances
( id INT PRIMARY KEY
, when DATE
, play VARCHAR(255)
, theatre VARCHAR(255)
)
Code:
101 2001-03-01 Macbeth Stratford
456 2002-04-27 Hamlet London
438 1945-03-03 Macbeth New York
Code:
Hamlet 2002-04-27 London 1
Macbeth 1945-03-03 New York 2
Code:
SELECT play, MIN(when), theatre, COUNT(*)
FROM performances
GROUP BY play
As a work around I am running a query to get a list of plays and then running a separate query for each play to find the MIN(when) and theatre. This takes a long time as there are thousands of plays!
Can anyone suggest how I can get the desired result with one SQL statement?
I am using MySQL v5.0.13
Andrew
Hampshire, UK