2011-10-14 5 views
1

Salut J'ai la procédure suivante magasinRegroupement résultat de la procédure stockée

`Utilisons [BD_SSEGUA] GO /* Objet: StoredProcedure [dbo] [spAgendaDeSolicitudes] Script Date:. 10/14/2011 16:43:00 */ SET ANSI_NULLS SUR GO SET quoted_identifier GO - ======================== ==================== - Auteur: Roque Ramírez Nájera - date de création: 23/03/2011 - Description: Genera tabla de sollicitudes - por y estatus año - spAgendaDeSolicitudes '2010' - ========== ===================================

ALTER PROCEDURE [dbo].[spAgendaDeSolicitudes] 

@anio varchar(5) 
AS 
    DECLARE 

    @ContR  int, 
    @ContRA  int, 
    @ContRZ  int, 
    @ContB  int, 
     @ContC  int, 
    @total  int 

    DECLARE 

    @agenda table (periodo datetime, R int, A int, RZ int, B int, C int, TOTAL int) 


     BEGIN 



SET NOCOUNT ON; 

SELECT @ContR = COUNT (fiIdSolicitud) FROM Solicitud WHERE fiEdoSolicitud = 1 and fiAnioSolicitud = @anio 
SELECT @ContRA = COUNT (fiIdSolicitud) FROM Solicitud WHERE fiEdoSolicitud = 2 and fiAnioSolicitud = @anio 
SELECT @ContRZ = COUNT (fiIdSolicitud) FROM Solicitud WHERE fiEdoSolicitud = 3 and fiAnioSolicitud = @anio 
SELECT @ContB = COUNT (fiIdSolicitud) FROM Solicitud WHERE fiEdoSolicitud = 4 and fiAnioSolicitud = @anio 
SELECT @ContC = COUNT (fiIdSolicitud) FROM Solicitud WHERE fiEdoSolicitud = 5 and fiAnioSolicitud = @anio 

SET @total = @ContR + @ContRA + @ContRZ + @ContB + @ContC 

INSERT INTO @agenda (R, A, RZ, B, C, TOTAL) 
VALUES(@ContR,@ContRA,@ContRZ,@ContB,@ContC,@total) 

SELECT R, A, RZ, B, C, TOTAL FROM @agenda END 

`

I utiliser ce sp pour remplir un télégramme radivid la procédure stockée obtient par conséquent le nombre de requêtes par année triées par statut, ce résultat est renseigné dans un telgrid radgrid. R est enregistré pour A est autorisé pour RZ est pour rejeté et ainsi de suite.

Ce que je veux faire est de regrouper les résultats par année, mois, semaine de l'année en cours. Mais le seul champ que j'ai est un datetime qui correspond à la date d'enregistrement qui est dans une autre table.

Comment puis-je résoudre ce problème? J'espère que vous m'aidez.

Répondre

1

pour regrouper vos données, vous pouvez créer un CTE d'ajouter année/mois/semaine correspondant au champ datetime et faites votre fonction d'agrégation dans le CTE

ici un exemple où je suppose que votre table temporaire @agenda contient toutes les données nécessaires:

;WITH myCTE AS 
(
    SELECT 
     periodo, 
     YEAR(periodo) AS yearPart, 
     MONTH(periodo) AS monthPart, 
     DATEPART(WEEK, periodo) AS weekPart, 
     R, A, RZ, B, C, 
     TOTAL int 
    FROM @agenda 
) 
SELECT 
    yearPart, monthPart, weekPart, 
    SUM(R) AS R, SUM(A) AS A, SUM(RZ) as RZ, SUM(B) AS B, SUM(C) AS C, 
    SUM(TOTAL) AS TOTAL 
FROM myCTE 
GROUP BY yearPart, monthPart, weekPart 

Hope this helps :)

Questions connexes