sql - counting records from a grouped query with additional criteria -
I have a table of reports containing these relevant fields:
user_id ( Int 11) There are several rows in user_id , some submitted with some approved_flag = 0 and some data submitted with (datetime) accepted_flag (shortcut 1) Approved_ flag = 1 and each with a unique submitted_date .
I need to get the count of approved and unacceptable reports. But I should only accept accepted_flag <120>
Such records:
user_id, submitted_date, accepted_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 (Use of this record )
Count 2 will be accepted and no one will be rejected, and if we have
Count 1 will be accepted (User ID 2) and 1 approved (User ID 1) .
To summarize once more because it is not easy to describe: I want the total number of unique users in the table (regardless of # lines for every user) and I want to 'Approved' and 'No No Ved' are based on approved_flag of the most recent report per user.
Any help would be greatly appreciated, thanks!
SELECT Approved_ Flag, from COUNT (*) join table T inner (SELECT user_id, Submitted_date = MAX (submitted_date) by table group user_id) Latest Latest .user_id = t.user_id and the latest. Submitted_date = t.submitted_date GROUP accepted_ flag
Comments
Post a Comment