对存储过程的结果进行分组
你好,我有下一个商店程序
`使用[BD_SSEGUA] 去 /* 对象:StoredProcedure [dbo].[spAgendaDeSlicitudes] 脚本日期:10/14/2011 16:43:00 < /em>*/ 设置 ANSI_NULLS 为开 去 将 QUOTED_IDENTIFIER 设置为 ON 去 -- =============================================== ——作者:罗克·拉米雷斯·纳胡拉 -- 创建日期:2011年3月23日 -- 描述:Genera tabla de solicitudes -- 过去的状态 -- spAgendaDeSolicity'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 表示“拒绝”等。
我想做的是将当年、每月、每周的结果分组。 但我拥有的唯一字段是与另一个表中的注册日期相对应的日期时间。
我该如何解决这个问题? 希望您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要对数据进行分组,您可以创建一个 CTE 来添加与日期时间字段相对应的年/月/周,并在 CTE 中创建聚合函数,
这里是一个示例,我假设您的临时表 @agenda 包含所有必要的数据:
希望这个有帮助:)
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:
Hope this helps :)