+ 4

Which is the easiest way to make a query with multiple subtotals?

I have a table with the following characteristics: Curso: itemDocente varchar(10) codigoCurso varchar(20) pagoDocente float tipoCurso varchar(1) mesCurso varchar(10) A teacher has many courses. A course can be of 3 types: C, F or M. I made a query that returns all the courses of a teacher grouping them to get the totals to pay, but wanted to know if you can get the sum of each type of course per teacher at a time. SELECT itemDocente, CourseCode, round (payDocente, 2) as C FROM reporte_pagos where mesCourse like '$mes' GROUP BY itemDocente, codeCourse UNION SELECT itemDocente, null, round (sum (paymentDocente), 2) FROM reporte_pagos where mesCourse like '$mes' GROUP BY itemDocente Order by itemDocente, codeDescription desc

11th Jul 2017, 9:21 PM
Victor Manuel Hernandez
Victor Manuel Hernandez - avatar
3 Antworten
0
Good attempt Victor. But based on your requirement, you need to use analytic functions. e.g. select r.*, sum(paymentDocente) over (partition by itemDocente order by itemDocente, codeDescription desc) FROM reporte_pagos r where mesCourse like '$mes' here, partition by creates a virtual grouping and the output is all rows with all columns and an extra column for the sum. you can have multiple sums
13th Jul 2017, 6:28 PM
Gurpreet Singh
Gurpreet Singh - avatar
+ 2
and i try your answer but my version of mysql have a error. my mysql is 3.23.32
13th Jul 2017, 9:05 PM
Victor Manuel Hernandez
Victor Manuel Hernandez - avatar
+ 1
my code is just an example, is not complete and might have issues in Mysql as this Oracle syntax. you need to understand analytic functions and their syntax in MySql
13th Jul 2017, 9:45 PM
Gurpreet Singh
Gurpreet Singh - avatar