I am very new to using SQL and I haven’t been able to find any previous posts that seem to answer my question. I need a statement that will give me the latest approved submission for every customer in the database and all rows/columns of data associated with that submission. Each submission is assigned a unique id number and is in a column called ASMT_ID. The unique customer id number is a column called PRTPT_ID.
So, for each PRTPT_ID in the database T_FINANCIAL_ITEM, I want to find the MAX ASMT_ID for each customer and display all columns and rows of data associated with each PRTPT_ID, but only if the STTS_NAME is equal to “Approved”.
In my simple terms what I’m trying to do:
select * from T_FINANCIAL_ITEM
For each PRTPT_ID SELECT MAX ASMT_ID
WHERE STTS_NAME = 'Approved'
GROUP by PRTPT_ID
*UPDATE*
Rather than using the asmt_id, I will need to analyze two other fields in the same table FYE_DT and SUBM_TYPE.
The criteria is:
- The most recent FYE_DT with a stts_name ‘Approved’ (i.e. 2011 before 2010).
- In the most recent fye_dt, the audited subm_type
- In the most recent fye_dt, if their is no audited subm_type, the unaudited subm_type
- It will pull all rows of data associated with the submission meeting the criteria
For example:
PRTPT_ID ASMT_ID FYE_DT SUBM_TYPE_NAME
8493000000 18016 30-JUN-09 12.00.00.000000000 AM Unaudited/A-133
8493000000 19574 30-JUN-09 12.00.00.000000000 AM Audited/A-133
8493000000 28039 30-JUN-10 12.00.00.000000000 AM Unaudited/A-133
8493000000 33620 30-JUN-10 12.00.00.000000000 AM Audited/A-133
9481000000 38049 31-DEC-10 12.00.00.000000000 AM Unaudited/Non-A133
9481000000 58020 31-DEC-09 12.00.00.000000000 AM Audited/Non-A-133
9481000000 48139 31-DEC-11 12.00.00.000000000 AM Unaudited/Non-A-133
Result:
PRTPT_ID ASMT_ID FYE_DT SUBM_TYPE_NAME
8493000000 33620 30-JUN-10 12.00.00.000000000 AM Audited/A-133
9481000000 48139 31-DEC-11 12.00.00.000000000 AM Unaudited/Non-A-133
Note: There can be multiple rows of data associated with these results. I need to have all of the rows of data.
One relatively standard approach would be to use analytic functions. If I understand your update, it sounds like you want the
ORDER BYclause to be something like