Hi I have the next store procedure
`USE [BD_SSEGUA] GO /* Object: StoredProcedure [dbo].[spAgendaDeSolicitudes] Script Date: 10/14/2011开发者_开发技巧 16:43:00 */ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: Roque Ramírez Nájera -- Create date: 23/03/2011 -- Description: Genera tabla de solicitudes -- por estatus y 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 use this sp to fill a telerik radgrid the stored procedure gets as a result the count of requests per year sorted by status, this result is populated in a telerik radgrid. R is for Registered A is for Authorized RZ is for Rejected and so on.
What I want to do is to group the results per year,month,week from the current year. But the only field that I have is a datetime that corresponds the registration date which is in another table.
How can I solve this? Hope your help.
to group your data, you can create a CTE to add year/month/week corresponding to the datetime field and make your aggregate function in the CTE
here an example where I suppose that your temp table @agenda contains all the necessary data:
;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 :)
精彩评论