简体   繁体   中英

add column in table1 derived calculation from table2 's column

I have two tables

  • Employee

  • Finance

Employee contains 3 columns

  1. id
  2. Name
  3. BaseSalary

Finance table contains 2 columns

  1. id
  2. BonusPercentage

I want to add TotalBonus column in Employee table. For calculation I am using BaseSalary from Employee table and BonusPercentage from Finance table.

TotalBonus = BaseSalary + (BaseSalary * BonusPercentage)

I can use this with stored procedure but i don't want. However, I created scalar function (calculateBonus) which returns TotalBonus but the problem is that i can't add column in table as i have dependency of BonusPercentage of Finance table.

In general i want employee table to have following:

  1. id
  2. Name
  3. BaseSalary
  4. TotalBonus

I know there are many ways but want in Employee table as I am using this table on C#. Thanks

Assuming that there can be a maximum of one row in finance for each employee, you want a (left) join:

SELECT e.id,
       e.name,
       e.basesalary,
       coalesce(f.bonuspercentage, 0) * e.basesalary totalbonus
       FROM employee e
            LEFT JOIN finance f
                      ON f.id = e.id;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM