mysql - How to use value from two different rows of a table in another table -


i have mysql table following structure , data:

increments  id   emp_id     starting_salary    increment_rate   increment_frequency 2     340            5000                250                  1 3     340            5000                250                  4 

i need have aliases, a , b hold value based on following formula:

starting_salary + (increment_rate * increment_frequency) 

to precise, want a = 5250 (based on a = (5000 + (250 * 1))) , b = 6000 (based on b = (5000 + (250 * 4)))

now have table following data:

 payslips     id        employee_id       salary_month             arrear   173824         340           '2015-06-01'              2350  

i want join a , b got table increments table payslips. , want use a , b in following way:

((a * 8) / 30 + (b * 22) / 30) 

my alias basic_salary. basic_salary hold value above calculation:

basic_salary = ((a * 8) / 30 + (b * 22) / 30)  = ((5250 * 8) / 30 + (6000 *22) / 30)  = (1400 + 4400)  = 5800 

i've got no idea how this. can please me?

all got common columns in both tables - emp_id , employee_id , can join both tables using these columns. can't figure out how can store above values , organize calculation inside query.

sample query:

select x.id, x.employee_id,  (*my calculation using , b table increments*) basic_salary, x.salary_month, x.arrear payslips x join increments y on x.employee_id = y.emp_id  

for determining a:

select  ( starting_salary +  (increment_rate * increment_frequency) ) increments id = 2 

and determining b:

select  ( starting_salary +  (increment_rate * increment_frequency) ) b increments id = 3 

mysql version: 5.2

i'm not clear on details, example should happen if there 3 rows 1 employee in increments? anyhow, here's sketch start with:

select employee_id      , ((a * 8) / 30 + (b * 22) / 30) basic_salary  (     select x.employee_id          , min(starting_salary + (increment_rate * increment_frequency))          , max(starting_salary + (increment_rate * increment_frequency)) b          , x.salary_month, x.arrear      payslips x      join increments y          on x.employee_id = y.emp_id      group x.employee_id, x.salary_month, x.arrear ) t 

if id 2 , 3 criteria (i assumed examples) can use case statement like:

 select employee_id       , ((a * 8) / 30 + (b * 22) / 30) basic_salary   (      select x.employee_id           , max(starting_salary + (increment_rate * case when y.id = 2 increment_frequency end ))           , max(starting_salary + (increment_rate * case when y.id = 3 increment_frequency end)) b           , x.salary_month           , x.arrear       payslips x       join increments y           on x.employee_id = y.emp_id       group x.employee_id, x.salary_month, x.arrear ) t; 

in case not matter aggregate use, rid of row contains null.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -