So I have this table named
maid_processing
id code confirm_arrival category_id arrived
5 A1 2014-01-02 NULL 1
4 B2 2014-01-02 NULL 1
3 A1 2014-01-01 NULL 1
2 B2 2014-01-01 NULL 1
1 B2 2014-01-01 NULL 0
So as you can see, the codes are repeated but other columns might differ. What I want to achieve is that I want to
Select all codes where the confirm_arrival field satisfies a given range of date, and arrived=1, and since A1 and B2 will be repeated twice since the example satisfies the condition Then we can group those with same code together, hence, 2 groups are formed, A1 and B2
So what I ultimately want is that within these 2 groups, I only want the row of input within each groups with the id number=biggest(hence latest).
The desired result will be
id code confirm_arrival
5 A1 2014-01-02
4 B2 2014-01-02
What I have so far will return only the earliest(id=smallest within each group), see the code below
SELECT *
FROM
( SELECT *
FROM maid_processing mp
LEFT
JOIN
( SELECT id catid
, archived
FROM category
) AS cat
ON cat.catid = maid_mp.category_id
WHERE arrived = 1
AND (archived IS NULL OR archived = 0)
AND confirm_arrival BETWEEN :from_date AND :to_date
ORDER
BY confirm_arrival DESC
) AS inv
GROUP
BY code;