Skip to content
Advertisement

SQL Query scenario

I have a SQL scenario. Could you please help me with the solutions for the same?

I have 2 Tables Account & Credit_Score, with the schema as below.

    -------------
    Account Table
    -------------
    Account_Number  Score
    A1              600
    A2              600
    B1              700
    B2              700
    B3              700
    C1              800
    C2              800

    -------------------
    Credit_Score Table
    -------------------
    Score          Balance
    600            1000
    700            6000
    800            8000

Now, I want a query which can give me a output as shown below. The balance amount gets splitted amongst the Account Holders having similar Score.

    ------------------------------
    Account_Number   |     Balance
    ------------------------------
    A1                     500
    A2                     500
    B1                     2000
    B2                     2000
    B3                     2000
    C1                     4000
    C2                     4000

Advertisement

Answer

You can use window functions:

select a.*,
       cs.balance / count(*) over (partition by cs.score) as balance
from account a join
     credit_score cs
     on cs.score = a.score;
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement