Filter By Dates in SQL

1

I have a somewhat confusing problem.

First: I want to know how many days of payment correspond to a person according to their assistance within the current period.

Table usuarios :

id  Nombre fecha_ingreso
1   Juan   01-01-2018
2   Pedro  01-01-2018

Table asistencias , where 1 is assistance and 0 is missing:

id asist fecha
1  0     02-08-2018
1  1     03-08-2018
2  1     02-08-2018
2  1     03-08-2018

My table periodo contains the following info:

idp  diai        diaf
1    30-07-2018  12-08-2018

My script (working correctly, Note: this is in SQL server):

$conn ~~ aqui se conecta a la base de datos ~~
$sql = "SELECT * FROM usuarios ORDER BY id ASC";
$result = sqlsrv_query($conn, $sql);
while ($usuario = sqlsrv_fetch_array($result)) {
$id=$usuario['id'];

  $sql_periodo_inicial = sqlsrv_query($conn, "SELECT top 1 diai FROM periodo order by diai DESC");
   if($t_periodo_c=sqlsrv_fetch_array($sql_periodo_inicial)) {
                                $t_perido_diai=$t_periodo_c['diai'];

                            } 
  $sql_periodo_inicial = sqlsrv_query($conn, "SELECT top 1 diaf FROM periodo order by diaf DESC");
   if($t_periodo_c=sqlsrv_fetch_array($sql_periodo_inicial)) {
                                $t_perido_diaf=$t_periodo_c['diaf'];
                            }  


$sql_asistencia_asistidas2 = "SELECT * FROM asistencia 
                where id=$id 
                and fecha >= '$t_perido_diai' 
                and fecha <= '$t_perido_diaf' 
                and asistencia = '1'";
$params = array();
$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$stmt2 = sqlsrv_query( $conn, $sql_asistencia_asistidas2 , $params, $options );
$dias_trabajados_ttAsist = sqlsrv_num_rows( $stmt2 ); 

echo $dias_trabajados_ttAsist;
}

With this in my table it correctly shows me the results Juan 1 attendance and Pedro 2 within the current period

Question

Second : If Andrés ( id=3 ) entered after the cut-off date (' diaf' = 12 ) but before the fortnight that would be day 15 for example on August 13, 2018.

This person would have 2 days worked that will not be paid on day 15 but will be added to his next fifteen of day 30.

What should I add to my query to find Andrew's additional days?:

$sql_asistencia_asistidas2 = "SELECT * FROM asistencia 
                where id=$id 
                and fecha >= '$t_perido_diai' 
                and fecha <= '$t_perido_diaf' 
                and asistencia = '1'";

This is so that in my table you can automatically generate this value, but more important to be able to identify if there is an error when registering users.

    
asked by claus 16.08.2018 в 18:21
source

0 answers