SQL - Consulta ordenadas por mes calendario

 
Vista:

Consulta ordenadas por mes calendario

Publicado por Damian (26 intervenciones) el 21/07/2009 18:36:30
Tengo una consulta realizada en SQL 2005 que me trae los totales por mes segun el año que el usuario ingrese(aca lo puse a mano) de los ingresos y egresos de un comercio, pero claro yo quiero que me traiga Enero,Febrero,Marzo,etc.
Y la consulta me trae en este caso(que son los meses que se fueron cargando)....Abril,Julio y Junio. Cuando tendria que ser Abril,Junio y Julio....Obviamente que el order by por Month(Fecha) no me sirve. La consulta es esta:

select case Month(Fecha) when 1 then 'Enero' when 2 then 'Febrero' when 3 then 'Marzo'when 4 then 'Abril' when 5 then 'Mayo' when 6 then 'Junio' when 7 then 'Julio' when 8 then 'Agosto'when 9 then 'Septiembre' when 10 then 'Octubre' when 11 then 'Noviembre' when 12 then 'Diciembre' end[Mes],sum(Importe)[Total Mes],M.Tipo From Movimientos M where M.Tipo in('VTA','ND','CC') and year(Fecha)=2009 group by Month(Fecha),M.Tipo
union
Select Case Month(Fecha) when 1 then 'Enero' when 2 then 'Febrero' when 3 then 'Marzo'
when 4 then 'Abril' when 5 then 'Mayo' when 6 then 'Junio' when 7 then 'Julio' when 8 then 'Agosto'
when 9 then 'Septiembre' when 10 then 'Octubre' when 11 then 'Noviembre' when 12 then 'Diciembre' end[Mes],sum(Importe)[Total Mes],M.Tipo From Movimientos M where M.Tipo in('PP','NC','PS','GA','PC') and year(Fecha)=2009 group by Month(Fecha),M.Tipo
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder