Am trying to resolve the below query. Please could you help?
BANKTABLE BT
AccountNumber AccountTypeCode Balance ShareAccountHolderCount
10800 2146 160 5
10800 2148 126 3
10800 2149 84 6
10800 2147 66 7
11039 2581 42 1
11039 2582 32 21
11039 2579 10 91
11039 2578 7 10
Requirement:
1. A Account Number(BT.AccountNumber) might have accounts in multiple AccountTypes(BT.AccountTypeCode) and
for the each account type he might have the different balances(BT.Balance).
2. Under the account for multiple accounttypes, he might have number of Shared account holders(BT.ShareAccountHolderCount)
3. I don’t have another table to join with the BANKTABLE
Question: Now I have to pick up the the Max(Balance) and its ShareAccountHolderCount for the account number from the all the account types
The Result Should look like as below.
AccountNumber AccountTypeCode Balance ShareAccountHolderCount
10800 2146 160 5
11039 2581 42 1
As per my research
Answer 1: TOP 1 and Order by Balance DESC Won’t work for this requirement.
Answer 2: Am trying to do something by writing below query, But not able to achieve the answer
SELECT AccountNumber,AccountTypeCode,Balance,ShareAccountHolderCount
,RowNumber = ROW_NUMBER() OVER (PARTITION BY AccountNumber ORDER BY LongestStressdays DESC)
,RowNumber2 = ROW_NUMBER() OVER (PARTITION BY AccountNumber ORDER BY CountOfTestTypeIdsperSequence DESC)
FROM BANKTABLE BT
You are near of solution. Second part consist in filter rows to get only the first row by partition: