I have three tables:
Table 1 – Members
Id Name
1 Member1
2 Member2
Table 2 – Activity
Id Member_id Activity Date
1 1 Activity1 2001-1-1
2 1 Activity2 2001-2-1
3 1 Activity3 2001-3-1
4 2 Test 2010-4-1
Table 3 – Detailed
Id Activity_id Information
1 2 Information
From the following, the Member id 1 has three activities:
SELECT Member.id, COUNT(*) AS number
FROM Member
INNER JOIN Activity ON Activity.Member_ID = Member.id
GROUP BY Member.id
My question is how I can find the id and date of the second activity (table 2) for all Members from table(1) who has 2 activities and also retrieve the information related to this activity id from table 3.
Thanks in advance
Row number function would help you here.
Here I’ve partitioned the data by member id and the ordered the activity by its id but maybe you want to order by date?