嗨,我有下一个商店程序
`USE [BD_SSEGUA] 走 / * 对象:StoredProcedure [dbo]。[spAgendaDeSolicitudes]脚本日期:10/14/2011 16:43:00 < / EM> * / SET ANSI_NULLS ON 走 SET QUOTED_IDENTIFIER ON 走 - ============================================= - 作者:RoqueRamírezNájera - 创建日期:23/03/2011 - 描述:Genera tabla de solicitudes - por estatusyañ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
`
我使用此sp来填充telerik radgrid,因此存储过程会获得按状态排序的每年请求数,此结果将填充在telerik radgrid中。 R代表注册 A适用于授权 RZ用于拒绝等等。
我想要做的是将每年,每月,每周的结果分组。 但我唯一的字段是与另一个表中的注册日期相对应的日期时间。
我该如何解决这个问题? 希望你的帮助。
答案 0 :(得分:1)
要对数据进行分组,您可以创建一个CTE来添加与datetime字段对应的年/月/周,并在CTE中创建聚合函数
这里有一个例子,我假设你的临时表@agenda包含所有必要的数据:
;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
希望这会有所帮助:)