简体   繁体   中英

ACCESS SQL : How to calculate wage (multiply function) and count no.of working days (count, distinct) of each staff between 2 dates

I need to create a form to summarize wage of each employees according to Date_start and Date_end that I selected.

I have 3 related tables.

tbl_labor
 lb_id | lb_name  | lb_OT ($/day) | If_social_sec
   1   |  John    |    10         |    yes
   2   |  Mary    |    10         |    no

tbl_production 
pdtn_date  | lb_id  | pdtn_qty(pcs) | pd_making_id 
 5/9/12    |   1    |     200       |    12
 5/9/12    |   1    |     40        |    13
 5/9/12    |   2    |     300       |    12
 7/9/12    |   1    |     48        |    13
 13/9/12   |   2    |     220       |    14
 15/9/12   |   1    |     20        |    12
 20/9/12   |   1    |     33        |    14
 21/9/12   |   2    |     55        |    14
 21/9/12   |   1    |     20        |    12

tbl_pdWk_process 
pd_making_id | pd_cost($/dozen) | pd_id 
    12       |     2            |   001
    13       |     5            |   001
    14       |     6            |   002

The outcome will look like this:

lb_name | no.working days | Total($)| OT payment | Social_sec($)| Net Wage             | 
  John  |        4        |  98.83  |  20 (2x10) |     15       |  103.83 (98.83+20-15)|
  Mary  |        2        |  160    |  10 (1x10) |     0        |  170 (160+10-0)      | 

My conditions are:

  1. Wage are calculate between 2 dates that i specify eg. 5/9/12 - 20/9/12
  2. Wage must be calculated from (pd_cost * pdtn_qty). However, pdtn_qty was kept in 'pieces' whereas pd_cost was kept in 'dozen'. Therefore the formula should be (pdtn_qty * pd_cost)/12
  3. Add OT * no. of OT days that each worker did (eg. John had 2 OT days, Mary 1 OT day)
  4. Total wages must be deducted eg. 15$ if If_social_sec are "TRUE"
  5. Count no. of working days that each employees had worked.

I've tried but i couldn't merge all this condition together in one SQL statement, so could you please help me. Thank you.

OK this is really messy. Mainly because Access has no COUNT(DISTINCT ) option. So counting the working days is a mess. If you can skip that, then you can drop all the pdn1,pdn2,pdn3 stuff. But id does work. Couple of notes 1. I think your maths is not quite right in the example given, I make it like this: 在此处输入图片说明

2 I've used the IIF clause to simulate 2 OT for john, 1 for mary. You'll need to change that. Good luck.

   select
  lab.lb_name,
  max(days),
  sum(prod.pdtn_qty * pdWk.pd_cost / 12) as Total ,
  max(lab.lb_OT * iif(lab.lb_id=1,2,1)) as OTPayment,
  max(iif(lab.if_social_sec='yes' , 15,0 )  ) as Social_Sec,
  sum(prod.pdtn_qty * pdWk.pd_cost / 12.00) +
  max(lab.lb_OT * iif(lab.lb_id=1,2,1)) -
  max(iif(lab.if_social_sec='yes' , 15,0 )  ) as NetWage
from
  tbl_labor as lab,
  tbl_production as prod,
  tbl_pdWk_process as pdwk,

(select pdn2.lb_id, count(pdn2.lb_id) as days from
  (select  lb_id
  from tbl_production  pdn1
  where pdn1.pdtn_date >= #9/5/2012#
  and   pdn1.pdtn_date <= #2012-09-20#
  group by lb_id, pdtn_date ) as pdn2
     group by pdn2.lb_id) as pdn3

where prod.pdtn_date >= #9/5/2012#
and   prod.pdtn_date <= #2012-09-20#
and prod.lb_id = lab.lb_id
and   prod.pd_making_id = pdwk.pd_making_id
and lab.lb_id = pdn3.lb_id
group by lab.lb_name

OK to add the items not in production table, you'll need to append something like this:

Union
select lab.lb_name,
0,
0,
max(lab.lb_OT * iif(lab.lb_id=1,2,1)) ,
max(iif(lab.if_social_sec='yes' , 15,0 )  ),0
from tbl_labor lab
where lb_id not in ( select lb_id from tbl_production   where pdtn_date >= #2012-09-05#      and   pdtn_date <= #2012-09-20#  )
group by lab.lb_name

Hope this helps.

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