对存储过程的结果进行分组

发布于 2024-12-09 16:42:01 字数 1850 浏览 0 评论 0原文

你好,我有下一个商店程序

`使用[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2024-12-16 16:42:01

要对数据进行分组,您可以创建一个 CTE 来添加与日期时间字段相对应的年/月/周,并在 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

希望这个有帮助:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文