Hola a todos, tengo el siguiente sp, lo que hace es mostrar las notas de un
curso-materia, el promedio de la primera y segunda etapa y un promedio final
El problema es en la columna PromFinal(hecha en base a 2 subconsultas), que
si en alguna de las dos etapas el alumno no tiene notas, el PromFinal
aparece con NULL, lo que quiero es que aparezca
en PromFinal el promedio de la etapa correspondiente.
CREATE PROCEDURE pa_select_notas_por_cur_mat_1y2etapa (@Curso as char(3),
@IdMateria as int) AS
SELECT A.Apellidos+', '+A.Nombres as Alumno,
CASE WHEN N1.IDEtapa = 1 THEN N1.Nota ELSE NULL END AS Nota1Et,
CASE WHEN N1.IDEtapa = 2 THEN N1.Nota ELSE NULL END AS Nota2Et,
CASE WHEN N1.IDEtapa = 1 THEN CONVERT(char(10),N1.FechaExamen,103) ELSE ''
END AS FechaExamen1Et,
CASE WHEN N1.IDEtapa = 2 THEN CONVERT(char(10),N1.FechaExamen,103) ELSE ''
END AS FechaExamen2Et,
E.Descripcion as Etapa, M.Nombre as Materia, N1.Curso,
Prom1Etapa = (SELECT ROUND(AVG(CAST(N2.Nota as FLOAT)),2)
FROM NOTAS N2
WHERE N2.CURSO = N1.CURSO AND N2.IDEtapa = N1.IDEtapa AND N2.IDMateria
= N1.IDMateria AND N2.IdAlumno = N1.IdAlumno AND N2.IDEtapa = 1),
Prom2Etapa = (SELECT ROUND(AVG(CAST(N2.Nota as FLOAT)),2)
FROM NOTAS N2
WHERE N2.CURSO = N1.CURSO AND N2.IDEtapa = N1.IDEtapa AND N2.IDMateria
= N1.IDMateria AND N2.IdAlumno = N1.IdAlumno AND N2.IDEtapa = 2),
PromFinal = ((SELECT ROUND(AVG(CAST(N2.Nota as FLOAT)),2)
FROM NOTAS N2
WHERE N2.CURSO = N1.CURSO AND N2.IDMateria = N1.IDMateria AND
N2.IdAlumno = N1.IdAlumno AND N2.IDEtapa = 1)
+
(SELECT ROUND(AVG(CAST(N2.Nota as FLOAT)),2)
FROM NOTAS N2
WHERE N2.CURSO = N1.CURSO AND N2.IDMateria = N1.IDMateria AND
N2.IdAlumno = N1.IdAlumno AND N2.IDEtapa = 2))/2
FROM NOTAS N1
INNER JOIN Materias M ON N1.IDMateria = M.IDMateria
INNER JOIN Alumnos A ON N1.IDAlumno = A.IDAlumno
INNER JOIN Etapas E ON N1.IDEtapa = E.IDEtapa
WHERE (N1.IDEtapa = 1 or N1.IDEtapa = 2) AND A.Curso = @Curso AND
N1.IdMateria = @IdMateria
ORDER BY A.Apellidos, A.Nombres, N1.IDEtapa
En la consulta se observa que 'JUAN PEREZ' NO posee notas en la segunda
etapa y si en la primera
(omití algunos campos no importantes que figuran en el selet):
Alumno Nota1Et Nota2Et Prom1Etapa Prom1Etapa
PromFinal
JUAN PEREZ 5 NULL 7.0 NULL
NULL
JUAN PEREZ 9 NULL 7.0 NULL
NULL
JUAN PEREZ 7 NULL 7.0 NULL
NULL
JUAN PEREZ 7 NULL 7.0 NULL
NULL
LEON GARCIA 4 NULL 8.0 NULL
7.5
LEON GARCIA 9 NULL 8.0 NULL
7.5
LEON GARCIA 8 NULL 8.0 NULL
7.5
LEON GARCIA 9 NULL 8.0 NULL
7.5
LEON GARCIA 10 NULL 8.0 NULL
7.5
LEON GARCIA NULL 5 NULL 7.0
7.5
LEON GARCIA NULL 8 NULL 7.0
7.5
LEON GARCIA NULL 8 NULL 7.0
7.5
Lo que me hace falta es:
Alumno Nota1Et Nota2Et Prom1Etapa Prom1Etapa
PromFinal
JUAN PEREZ 5 NULL 7.0 NULL
7.0
JUAN PEREZ 9 NULL 7.0 NULL
7.0
JUAN PEREZ 7 NULL 7.0 NULL
7.0
JUAN PEREZ 7 NULL 7.0 NULL
7.0
LEON GARCIA 4 NULL 8.0 NULL
7.5
LEON GARCIA 9 NULL 8.0 NULL
7.5
LEON GARCIA 8 NULL 8.0 NULL
7.5
LEON GARCIA 9 NULL 8.0 NULL
7.5
LEON GARCIA 10 NULL 8.0 NULL
7.5
LEON GARCIA NULL 5 NULL 7.0
7.5
LEON GARCIA NULL 8 NULL 7.0
7.5
LEON GARCIA NULL 8 NULL 7.0
7.5
Adjunto en un txt el mensaje, por las dudas las columnas de la consulta se
"amontonen" al verlas por el Outlook
Saludos y mil gracias.
PD: espero se entienda, no soy muy bueno dando explicaciones.
Por las dudas... estructura de la tabla Notas:
CREATE TABLE [dbo].[Notas] (
[IDAlumno] [int] NOT NULL ,
[IDMateria] [smallint] NOT NULL ,
[IDEtapa] [smallint] NOT NULL ,
[Curso] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[Orden] [int] IDENTITY (1, 1) NOT NULL ,
[Nota] [smallint] NOT NULL ,
[FechaExamen] [smalldatetime] NULL
) ON [PRIMARY]
begin 666 Consulta de promedios.txt
M#0I#4D5!5$4@4%)/0T5$55)%('!A7W-E;&5C=%]N;W1A<U]P;W)?8W5R7VUA
M=%\Q>3)E=&%P82 H0$-U<G-O(&%S(&-H87(H,RDL($!)9$UA=&5R:6$@87,@
M:6YT*2!!4PT*4T5,14-4($$N07!E;&QI9&]S*R<L("<K02Y.;VUB<F5S(&%S
M($%L=6UN;RP@#0I#05-%(%=(14X@3C$N241%=&%P82 ](#$@5$A%3B!.,2Y.
M;W1A($5,4T4@3E5,3"!%3D0@05,@3F]T83%%="P-"D-!4T4@5TA%3B!.,2Y)
M1$5T87!A(#T@,B!42$5.($XQ+DYO=&$@14Q312!.54Q,($5.1"!!4R!.;W1A
M,D5T+" -"D-!4T4@5TA%3B!.,2Y)1$5T87!A(#T@,2!42$5.($-/3E9%4E0H
M8VAA<B@Q,"DL3C$N1F5C:&%%>&%M96XL,3 S*2!%3%-%("<G($5.1"!!4R!&
M96-H845X86UE;C%%="P-"D-!4T4@5TA%3B!.,2Y)1$5T87!A(#T@,B!42$5.
M($-/3E9%4E0H8VAA<B@Q,"DL3C$N1F5C:&%%>&%M96XL,3 S*2!%3%-%("<G
M($5.1"!!4R!&96-H845X86UE;C)%="P@#0I%+D1E<V-R:7!C:6]N(&%S($5T
M87!A+"!-+DYO;6)R92!A<R!-871E<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QU;6YO(#T@3C$N261!;'5M;F\@04Y$
M($XR+DE$171A<&$@/2 R*2DO,@D)#0I&4D]-($Y/5$%3($XQ#0I)3DY%4B!*
M3TE.($UA=&5R:6%S($T@3TX@3C$N241-871E<FEA(#T@32Y)1$UA=&5R:6$-
M"DE.3D52($I/24X@06QU;6YO<R!!($].($XQ+DE$06QU;6YO(#T@02Y)1$%L
M=6UN;PT*24Y.15(@2D])3B!%=&%P87,@12!/3B @3C$N241%=&%P82 ]($4N
M241%=&%P80T*5TA%4D4@*$XQ+DE$171A<&$@/2 Q(&]R($XQ+DE$171A<&$@
M/2 R*2!!3D0@02Y#=7)S;R ]($!#=7)S;R!!3D0@3C$N261-871E<FEA(#T@
M0$ED36%T97)I82 -"D]21$52($)9($$N07!E;&QI9&]S+"!!+DYO;6)R97,L
M($XQ+DE$171A<&$-"@T*16X@;&$@8V]N<W5L=&$@<V4@;V)S97)V82!Qd@
M)TI504X@4$5215HG($Y/('!O<V5E(&YO=&%S(&5N(&QA('-E9W5N9&$@971A
M<&$@>2!S:2!E;B!L82!P<FEM97)A#0HH;VUI=.T@86QG=6YO<R!C86UP;W,@
M;F\@:6UP;W)T86YT97,@<75E(&9I9W5R86X@96X@96P@<V5L970I.@T*#0I!
M;'5M;F\)"0E.;W1A,45T($YO=&$R170)(%!R;VTQ171A<&$@4')O;3%%=&%P
M82!0<F]M1FEN86P-"DI504X@4$5215H)"34)3E5,3 D)-RXP"4Y53$P)3E5,
M3 T*2E5!3B!015)%6@D).0E.54Q,"0DW+C )3E5,3 E.54Q,#0I*54%.(%!%
M4D5:"0DW"4Y53$P)"3<N, E.54Q,"4Y53$P-"DI504X@4$5215H@"0DW"4Y5
M3$P)"3<N, E.54Q,"4Y53$P-"DQ%3TX@1T%20TE!"0DT"4Y53$P)"3@N, E.
M54Q,"3<N-0T*3$5/3B!'05)#24$)"3D)3E5,3 D)."XP"4Y53$P)-RXU#0I,
M14].($=!4D-)00D). E.54Q,"0DX+C )3E5,3 DW+C4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