I have a table of reports that has these relevant fields:
user_id (int 11)
submitted_date (datetime)
approved_flag (shortint 1)
There are multiple rows per user_id
, some with approved_flag
= 0 and some with approved_flag
= 1 and each with a unique submitted_date
.
I need to get a count of the approved and unapproved reports. But I only want to count the most recent submitted report per user (most recent submitted_date
), regardless of approved_flag
.
So, if there was 4 records like this:
user_id, submitted_date, approved_flag 1, 2009-04-01 01:00, 1 1, 2009-04-01 02:00, 0 1, 2009-04-01 03:00, 1 (using this record) 2, 2009-04-02 01:00, 1 (using this record)
the count would be 2 approved and no unapproved, and if we had
user_id, submitted_date, approved_flag 1, 2009-04-01 01:00, 1 1, 2009-04-01 02:00, 0 1, 2009-04-01 03:00, 0 (using this record) 2, 2009-04-02 01:00, 0 2, 2009-04-02 02:00, 1 (using this record)
the count would be 1 approved (user id 2) and 1 unapproved (user id 1).
To summarize one more time because it is not easy to describe: I want the total number of unique users in the table (regardless of # rows per user) and I want it broken down in to 'approved' and 'not approved' based on the approved_flag
of the most recent report per user.
Any help would be greatly appreciated, thanks!