List data omitting repeated and add them in MySQL

0

I have the following table:

Payments and the fields you have are idpagos , codigo_empleado , fecha_pago , trimestre , monto_pago

The payment quarter is because the employees are paid 15 and last of each month and I am identifying them as 1 for the first quarter and 2 for the second quarter.

What I want is to show me the entire list of the payments of each employee so that they do not repeat themselves and add the amounts paid from both quarters for each employee.

I tried this:

SELECT codigo_empleado, SUM(monto_pago)
FROM pagos WHERE MONTH(fecha_pago)=9 AND YEAR(fecha_pago)=2017

But it shows me only one employee_code and the sum of the total of all amounts paid from all employees in that month.

    
asked by Thovar190 12.10.2017 в 20:19
source

1 answer

0

Assuming that you want to group the total amounts of employee payments for each month (quarter, as you call it, 15 and last). You could do it this way to be grouped by month and year:

SELECT codigo_empleado, SUM(monto_pago) AS MontoTotal, MONTH(fecha_pago) AS MES, YEAR(fecha_pago) AS 'AÑO' 

FROM pagos

GROUP BY codigo_empleado, YEAR(fecha_pago), MONTH(fecha_pago)

It would be something like this:

However, it would be ideal to have more information about the structure of the tables and the information you are hosting in them to understand your question.

    
answered by 26.10.2017 в 22:24