mercredi 30 mars 2016

attendance table query Sqlite3

I am building a client attendance table with Pyhton3 / SQLITE3 and looking for a method to:

1- sum hours for each name at the end of the day

2- reset the counting to 0 when the total sum of hours are 40 or more. (I need to charge every client every 40 hours spent)

table_1 (everyday count)

day    Name    hours    
1      Jack    5
1      Jane    30
1      Jane    5
2      Jane    5
2      John    20
3      John    18
3      John    4

table_2
(before)

 Name    totalhours   code
 Jack    5
 Jane    40        
 John    42       

(after)

 Name    totalhours   code
 Jack    5
 Jane    0          66  <-   reset to "0" when count is >= 40 and mark code 66
 John    0          66

For step 1, fine with :

 'INSERT INTO table_2 (name, sum_hours) SELECT name, SUM(hours)  FROM table_1   GROUP BY name '

I am struggling in building step 2 ; I dont know if I can do it using the GROUP BY statement ?

My idea was to:

  • select total hours of table_2 for each name
  • if hours >= 40 (table_2) -> UPDATE to 0 or DELETE all rows 'hours' of (table_1) for each name
  • mark "0" / code 66 on (table_2) for each name that has spent more than 40hrs

Am I going in the right direction? any input is welcome

thanks david

Aucun commentaire:

Enregistrer un commentaire