24/12/2007, 05:27
|
| | | Fecha de Ingreso: abril-2005
Mensajes: 501
Antigüedad: 19 años, 9 meses Puntos: 3 | |
Re: Sacar cantidades totales por meses de trimestre Ya lo resolví finalmente.
Al final, metido todo en un Procedimiento, me queda así:
Código:
CREATE PROCEDURE [spSubInf_ListTrimClieRecog_MesLitros]
@trimestre INT,
@anio NVARCHAR(4),
@cliente INT
AS
IF (@trimestre = 1)
BEGIN
SELECT MONTH(@anio + '0101') AS NUM_MES, DATENAME(M, @anio + '0101') AS MES, ISNULL(SUM(ISNULL(litros, 0)), 0) AS Cantidad
FROM ALBARANES_C
WHERE cliente = @cliente
AND YEAR(fecha) = @anio AND MONTH(fecha) = 1
UNION
SELECT MONTH(@anio + '0201') AS NUM_MES, DATENAME(M, @anio + '0201') AS MES, ISNULL(SUM(ISNULL(litros, 0)), 0) AS Cantidad
FROM ALBARANES_C
WHERE cliente = @cliente
AND YEAR(fecha) = @anio AND MONTH(fecha) = 2
UNION
SELECT MONTH(@anio + '0301') AS NUM_MES, DATENAME(M, @anio + '0301') AS MES, ISNULL(SUM(ISNULL(litros, 0)), 0) AS Cantidad
FROM ALBARANES_C
WHERE cliente = @cliente
AND YEAR(fecha) = @anio AND MONTH(fecha) = 3
ORDER BY NUM_MES
END
ELSE IF (@trimestre = 2)
BEGIN
SELECT MONTH(@anio + '0401') AS NUM_MES, DATENAME(M, @anio + '0401') AS MES, ISNULL(SUM(ISNULL(litros, 0)), 0) AS Cantidad
FROM ALBARANES_C
WHERE cliente = @cliente
AND YEAR(fecha) = @anio AND MONTH(fecha) = 4
UNION
SELECT MONTH(@anio + '0501') AS NUM_MES, DATENAME(M, @anio + '0501') AS MES, ISNULL(SUM(ISNULL(litros, 0)), 0) AS Cantidad
FROM ALBARANES_C
WHERE cliente = @cliente
AND YEAR(fecha) = @anio AND MONTH(fecha) = 5
UNION
SELECT MONTH(@anio + '0601') AS NUM_MES, DATENAME(M, @anio + '0601') AS MES, ISNULL(SUM(ISNULL(litros, 0)), 0) AS Cantidad
FROM ALBARANES_C
WHERE cliente = @cliente
AND YEAR(fecha) = @anio AND MONTH(fecha) = 6
ORDER BY NUM_MES
END
ELSE IF (@trimestre = 3)
BEGIN
SELECT MONTH(@anio + '0701') AS NUM_MES, DATENAME(M, @anio + '0701') AS MES, ISNULL(SUM(ISNULL(litros, 0)), 0) AS Cantidad
FROM ALBARANES_C
WHERE cliente = @cliente
AND YEAR(fecha) = @anio AND MONTH(fecha) = 7
UNION
SELECT MONTH(@anio + '0801') AS NUM_MES, DATENAME(M, @anio + '0801') AS MES, ISNULL(SUM(ISNULL(litros, 0)), 0) AS Cantidad
FROM ALBARANES_C
WHERE cliente = @cliente
AND YEAR(fecha) = @anio AND MONTH(fecha) = 8
UNION
SELECT MONTH(@anio + '0901') AS NUM_MES, DATENAME(M, @anio + '0901') AS MES, ISNULL(SUM(ISNULL(litros, 0)), 0) AS Cantidad
FROM ALBARANES_C
WHERE cliente = @cliente
AND YEAR(fecha) = @anio AND MONTH(fecha) = 9
ORDER BY NUM_MES
END
ELSE IF (@trimestre = 4)
BEGIN
SELECT MONTH(@anio + '1001') AS NUM_MES, DATENAME(M, @anio + '1001') AS MES, ISNULL(SUM(ISNULL(litros, 0)), 0) AS Cantidad
FROM ALBARANES_C
WHERE cliente = @cliente
AND YEAR(fecha) = @anio AND MONTH(fecha) = 10
UNION
SELECT MONTH(@anio + '1101') AS NUM_MES, DATENAME(M, @anio + '1101') AS MES, ISNULL(SUM(ISNULL(litros, 0)), 0) AS Cantidad
FROM ALBARANES_C
WHERE cliente = @cliente
AND YEAR(fecha) = @anio AND MONTH(fecha) = 11
UNION
SELECT MONTH(@anio + '1201') AS NUM_MES, DATENAME(M, @anio + '1201') AS MES, ISNULL(SUM(ISNULL(litros, 0)), 0) AS Cantidad
FROM ALBARANES_C
WHERE cliente = @cliente
AND YEAR(fecha) = @anio AND MONTH(fecha) = 12
ORDER BY NUM_MES
END
GO
__________________ Saludos, zacktagnan.
================================================= |