I am new to Stored Procedure and I searched for the answer and could not find one working for me.
I have a table like below:
|---------------------|------------------|----------------------|
| InterfaceID | Prefix | FinancialID |
|---------------------|------------------|----------------------|
| 1501 | ADM/B/11/ | 8 |
|---------------------|------------------|----------------------|
| 1501 | ASD/K/10/ | 7 |
|---------------------|------------------|----------------------|
| 1502 | BDM/H/22/ | 7 |
|---------------------|------------------|----------------------|
| 1503 | CDM/Q/14/ | 6 |
|---------------------|------------------|----------------------|
| 1506 | FDM/R/17/ | 8 |
|---------------------|------------------|----------------------|
| 1506 | FDU/R/16/ | 7 |
|---------------------|------------------|----------------------|
| 1506 | FDT/R/14/ | 6 |
|---------------------|------------------|----------------------|
| 1506 | FDD/R/12/ | 5 |
|---------------------|------------------|----------------------|
| 1506 | LMD/R/10/ | 4 |
|---------------------|------------------|----------------------|
I need an output like this
|---------------------|------------------|----------------------|
| InterfaceID | Prefix | FinancialID |
|---------------------|------------------|----------------------|
| 1501 | ADM/B/11/ | 8 |
|---------------------|------------------|----------------------|
| 1502 | BDM/H/22/ | 7 |
|---------------------|------------------|----------------------|
| 1503 | CDM/Q/14/ | 6 |
|---------------------|------------------|----------------------|
| 1506 | FDM/R/17/ | 8 |
|---------------------|------------------|----------------------|
That is, if want to select the one row for each InterfaceID
with highest FinancialID
.
I tried this select query:
select InterfaceID,Prefix,FinancialID
from aInterfaceTable
WHERE Financialid=(select max(Financialid))
but it gives me an error
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
how to do this easily?