Concatenate multiple rows in a single mysql

1

I really do not know if it will be possible, I hope for answers

Join rows according to the name and name, doing the operation: (weight * note) / 100 at the end add each result and put it in a single row, leaving something like this:

nombre            nota
POO II            14.2
POO               14.4

And if there would be more units something like this:

nombre            nota1       nota2     nota3 
POO II            14.2
POO               14.4

select asignatura.nombre,unidad.nombreu,valoracion.nombrev,valoracion.peso,nota.nota 
from nota 
INNER JOIN asignatura on asignatura.codasig=nota.asignatura 
INNER JOIN unidad on unidad.id=nota.unidad 
INNER JOIN valoracion on valoracion.id=nota.valoracion 
where nota.estudiante='15104055' LIMIT 0, 30 ;


nombre  nombreu         nombrev       peso    nota
POO II  Primera Unidad  Examen         40     10
POO II  Primera Unidad  Practica       40     18
POO II  Primera Unidad  Actitudinal    20     15
POO     Primera Unidad  Examen         30     18
POO     Primera Unidad  Practica       50     10
POO     Primera Unidad  Actitudinal    20     20
    
asked by Rodolfo Gav 18.02.2017 в 21:05
source

3 answers

0

What you want to do is not possible since you want to list attributes of the same column as multiple independent columns. So I can think of two alternatives to offer you.

1) - You could group by name and have the sums of the notes:

SELECT CONCAT(nombre, " ", nombreu) as nombre, SUM(peso*nota/100) as nota
FROM nota 
INNER JOIN asignatura on asignatura.codasig=nota.asignatura 
INNER JOIN unidad on unidad.id=nota.unidad 
INNER JOIN valoracion on valoracion.id=nota.valoracion 
WHERE nota.estudiante='15104055'
GROUP BY nombre
LIMIT 0, 30 ;

2) - Obtain all records with their separate notes:

SELECT CONCAT(nombre, " ", nombreu) as nombre, peso*nota/100 as nota
    FROM nota 
    INNER JOIN asignatura on asignatura.codasig=nota.asignatura 
    INNER JOIN unidad on unidad.id=nota.unidad 
    INNER JOIN valoracion on valoracion.id=nota.valoracion 
    WHERE nota.estudiante='15104055'
    LIMIT 0, 30 ;

And then with logic of a programming language like PHP you make a table with the attributes.

Please note that it is figurative, you may have to adapt the names of the tables or attributes, but I leave it by way of example so that you can observe the use mode of the GROUP BY and the CONCAT() that you are going to be useful for what you need.

Greetings!

    
answered by 18.02.2017 / 21:10
source
0

try something like this:

    select n.nombre,
    SUM(case WHEN n.nombrev='Examen' then (n.peso*n.nota/100)END) as nota1,
    SUM(case WHEN n.nombrev='Practica' then (n.peso*n.nota/100)  END) as nota2,
    SUM(case WHEN n.nombrev='Actitudinal' then (n.peso*n.nota/100) END) as nota3
    from nota n
    INNER JOIN asignatura a on a.codasig=n.asignatura 
    INNER JOIN unidad u on u.id=n.unidad 
    INNER JOIN valoracion v on v.id=n.valoracion 
    where n.estudiante='15104055' 
    group by n.estudiante,n.nombre,u.nombreu
    LIMIT 0, 30 ;
    
answered by 19.02.2017 в 02:28
0

Observe this scheme, it could be improved:

You can add the sums you want, and try the queries in this sqlfidle .

You will have a line by subject and a column with all the notes of all the units of a student separated by a character (| in this case) if you want to divide each note in other columns you can do it by programming, doing split on the column notes. The advantage (among others) is that, if you do not know how many notes a student could have, it is best to have a single column where you receive all the notes and then divide them as you wish, knowing that there is a '|' between each one.

SQL Fiddle

MySQL 5.6 Schema Setup :

CREATE TABLE estudiantes
    ('id_estudiante' int, 'estudiante' varchar(70))
;

INSERT INTO estudiantes
    ('id_estudiante', 'estudiante' )
VALUES
    (1, 'Pedro'),
    (2, 'Santiago'),
    (3, 'Juan')
;


CREATE TABLE asignaturas
    ('id_asignatura' int, 'asignatura' varchar(70))
;

INSERT INTO asignaturas
    ('id_asignatura', 'asignatura')
VALUES
    (1, 'POO'),
    (2, 'POO II'),
    (3, 'Java')
;

CREATE TABLE unidades
    ('id_unidad' int, 'unidad' varchar(70))
;

INSERT INTO unidades
    ('id_unidad', 'unidad')
VALUES
    (1, 'Primera Unidad'),
    (2, 'Segunda Unidad'),
    (3, 'Tercera Unidad')
;


CREATE TABLE valoraciones
    ('id_valoracion' int, 'valoracion' varchar(70), 'peso' int)
;

INSERT INTO valoraciones
    ('id_valoracion', 'valoracion', 'peso' )
VALUES
    (1, 'Examen',40),
    (2, 'Práctica',40),
    (3, 'Actitudinal',20)
;


CREATE TABLE notas
    ('id' int, 'id_estudiante' int, 'id_asignatura' int, 'id_unidad' int, 'id_valoracion' int, 'nota' float)
;

INSERT INTO notas
    ('id', 'id_estudiante', 'id_asignatura', 'id_unidad', 'id_valoracion', 'nota')
VALUES
    (1, 1, 1, 1, 1, 14.2),
    (2, 1, 2, 1, 2, 14.4),
    (3, 2, 1, 1, 1, 13.4),
    (4, 2, 2, 1, 2, 13.9),
    (5, 1, 1, 2, 1, 10.2),
    (6, 1, 2, 2, 2, 18.4)


;

Query 1 :

SELECT 
  e.estudiante, a.asignatura, 
  v.valoracion, v.peso
  ,
  GROUP_CONCAT(n.nota SEPARATOR '|') AS nota
  FROM estudiantes e 
  JOIN notas n ON e.id_estudiante=n.id_estudiante
  JOIN asignaturas a ON n.id_asignatura=a.id_asignatura
  JOIN unidades u ON n.id_unidad=u.id_unidad
  JOIN valoraciones v ON v.id_valoracion=n.id_valoracion
  WHERE e.id_estudiante=1
  GROUP BY e.id_estudiante, a.id_asignatura

Results :

| estudiante | asignatura | valoracion | peso |      nota |
|------------|------------|------------|------|-----------|
|      Pedro |        POO |     Examen |   40 | 10.2|14.2 |
|      Pedro |     POO II |   Práctica |   40 | 14.4|18.4 |
    
answered by 19.02.2017 в 03:45