'Select unique records in sqlite from multiple tables

assume sqlite db with 2 tables:

member (columns: member_id, status )

action (columns: action_id, member_id, action_time)

A foreign key from member.member_id references action.member_id

action contains multiple actions preformed by multiple members on various times.

image of action table

A member may be active or inactive.

I am tryin to query all active members with no actions in the last week.

def nothing-this-week():
db = Db()
data = (week_ago,)
db.execute('SELECT member.*, action_action.member_id, action_action.action_time'
           ' from member'
           ' join action_action on member.member_id = action_action.member_id where '
           'member.status = "active" and action_action.action_time < ? '
           'GROUP BY member.member_id'
           , data)
return db.fetchall()

This query picks up all actions preformed a week ago even if there is a later action preformed by the same member. What I need is a way to isolate the latest action by a member in the action table.

However, I am quite a newbie and don't know how. Searched stack overflow for sometime but couldn't find anything helpful. Any assistance would be greatly appreciated



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source