How to join the two tables in this case?

Asked

Viewed 705 times

2

I have two tables a "students" with the enrollment and enrollment and another "faz_teste" with the grades of the students in certain tests. I used the pivot command in the table "faz_teste" to transform the name of the proofs into column and put the notes into rows, as shown in the following query:

SELECT  inscricao,[1_BIM],[2_BIM],[3_BIM],[4_BIM]
 FROM (
    SELECT inscricao, idteste, nota_num
    FROM [EasyNovo].[dbo].[faz_teste]
    WHERE idteste='1_BIM' OR idteste='2_BIM' OR idteste='3_BIM' OR idteste='4_BIM'
  )tt

PIVOT (SUM(nota_num) 
FOR idteste IN ([1_BIM],[2_BIM],[3_BIM],[4_BIM]))pvt

In the result of this query I have the registration and grades of the students in each test, as shown in the image:

inserir a descrição da imagem aqui

But I need to by, in addition to the inscription, the name. How to do in this case?

  • Have you tried SELECT inscricao, idteste, nota_numn, NOME and in the , PIVOT (SUM(nota_num) , NOME ..... SELECT inscricao,[1_BIM],[2_BIM],[3_BIM],[4_BIM], NOME ?

  • a Use JOIN as well

1 answer

0

Try to do.

SELECT  inscricao,[1_BIM],[2_BIM],[3_BIM],[4_BIM], Nome
 FROM (
    SELECT inscricao, idteste, nota_num, a.Nome
    FROM [EasyNovo].[dbo].[faz_teste] ft
    join [EasyNovo].[dbo].[alunos] a
    on a.idaluno = ft.idaluno
    WHERE idteste='1_BIM' OR idteste='2_BIM' OR idteste='3_BIM' OR idteste='4_BIM'
  )tt

PIVOT (SUM(nota_num) 
FOR idteste IN ([1_BIM],[2_BIM],[3_BIM],[4_BIM]))pvt

see the table link on a.idaluno = ft.idaluno

  • I tried that way, but it didn’t work.

  • which error gave? .

  • Only column title, not content.

  • you made the correct match in select "The Ids " on a.idaluno = ft.idaluno

  • Yes, look at it: FROM ( SELECT a.name, ft.inscription, ft.idteste, ft.nota_num FROM [Easynovo]. [dbo]. [faz_teste] ft Join [Easynovo]. [dbo]. [student] a on a.inscription = ft.inscription WHERE .....

  • This internal select is returning something?

  • I’ll go through all Query for you to see: SELECT nome, inscricao,[1_BIM],[2_BIM],[3_BIM],[4_BIM]
 FROM ( SELECT a.nome, ft.inscricao, ft.idteste, ft.nota_num
 FROM [EasyNovo].[dbo].[faz_teste] ft
join [EasyNovo].[dbo].[aluno] a
 on a.inscricao = ft.inscricao
 WHERE idteste='1_BIM' OR idteste='2_BIM' OR idteste='3_BIM' OR idteste='4_BIM'
 )tt

PIVOT (SUM(nota_num) 
FOR idteste IN ([1_BIM],[2_BIM],[3_BIM],[4_BIM]))pvt

  • try to see what returns in . SELECT a.nome, ft.inscricao, ft.idteste, ft.nota_num FROM [EasyNovo].[dbo].[faz_teste] ft join [EasyNovo].[dbo].[aluno] a on a.inscricao = ft.inscricao WHERE idteste='1_BIM' OR idteste='2_BIM' OR idteste='3_BIM' OR idteste='4_BIM'

  • Returns nothing.

  • well there you will have to see the relationship between these tables .

  • Got something?

  • Not yet. I’ll go back to stir today, I haven’t had much time. If I get any solution put here.

  • you have to review your join , make a select that returns the data of the two tables.

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.