I have a table as below, I want to extract only one record of every customer having data less than or equal to '06/30/2012' and the largest of the RowNum.
| RowNum | customer_ID | date | balance |
|---|---|---|---|
| 197 | BS:141723 | 6/30/2012 | 124693.08 |
| 195 | BS:165012 | 5/31/2012 | 26346.42 |
| 27 | BS:166289 | 6/30/2012 | 5253.67 |
| 41 | BS:209459 | 6/30/2012 | 32673.04 |
| 127 | BS:141723 | 6/30/2012 | 205849.11 |
| 15 | BS:192907 | 4/30/2012 | 106236.71 |
| 47 | BS:192907 | 5/31/2012 | 7430.6 |
| 97 | BS:165012 | 4/30/2012 | 721 |
The expected output should be:
| customer_ID | balance | RowNum |
|---|---|---|
| BS:141723 | 124693.08 | 197 |
| BS:165012 | 26346.42 | 195 |
| BS:166289 | 5253.67 | 27 |
| BS:192907 | 7430.6 | 47 |
| BS:209459 | 32673.04 | 41 |
I have written this query
select DISTINCT customer_ID, balance
, MAX(RowNum) over (PARTITION by customer_ID)
from Test
and the result which I am getting is below which is incorrect.
| customer_ID | balance | RowNum |
|---|---|---|
| BS:141723 | 124693.08 | 197 |
| BS:141723 | 205849.11 | 197 |
| BS:165012 | 721 | 195 |
| BS:165012 | 26346.42 | 195 |
| BS:166289 | 5253.67 | 27 |
| BS:192907 | 7430.6 | 47 |
| BS:192907 | 106236.71 | 47 |
| BS:209459 | 32673.04 | 41 |