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:
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 BY
clause to be something like
select *
from (select t.*,
rank() over (partition by prtpt_id
order by fye_dt desc,
(case when subm_type_name like 'Audited%'
then 2
when subm_type_name like 'Unaudited%'
then 1
else 0
end) desc) rnk
from t_financial_item t
where stts_name = 'Approved')
where rnk = 1
What's your table structure? This would appear to be what you're looking for:
select MAX(ASMT_ID),PRTPT_ID from PRTPT_ID_TABLE p
INNER JOIN ASMT_ID_TABLE a on p.PRTPT_ID = a.PRTPT_ID
WHERE STTS_NAME = 'Approved'
GROUP by PRTPT_ID
but I don't see any tables. Is T_FINANCIAL_ITEM the table or the database name?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.