sql server 2008 - Grouping stored procedure's result -


hi have 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)      declare       @contr     int,      @contra        int,      @contrz        int,      @contb     int,       @contc        int,      @total     int      declare       @agenda table ( periodo datetime, r int, int, rz int, b int, c int, total int)         begin    set nocount on;  select @contr = count (fiidsolicitud) solicitud fiedosolicitud = 1 , fianiosolicitud = @anio select @contra = count (fiidsolicitud) solicitud fiedosolicitud = 2 , fianiosolicitud = @anio select @contrz = count (fiidsolicitud) solicitud fiedosolicitud = 3 , fianiosolicitud = @anio select @contb = count (fiidsolicitud) solicitud fiedosolicitud = 4 , fianiosolicitud = @anio select @contc = count (fiidsolicitud) solicitud fiedosolicitud = 5 , fianiosolicitud = @anio  set @total = @contr + @contra + @contrz + @contb + @contc  insert @agenda  (r, a, rz, b, c, total) values(@contr,@contra,@contrz,@contb,@contc,@total)  select r, a, rz, b, c, total @agenda end 

`

i use sp fill telerik radgrid stored procedure gets result count of requests per year sorted status, result populated in telerik radgrid. r registered authorized rz rejected , on.

what want group results per year,month,week current year. field have datetime corresponds registration date in table.

how can solve this? hope help.

to group data, can create cte add year/month/week corresponding datetime field , make aggregate function in cte

here example suppose temp table @agenda contains necessary data:

;with mycte  (     select          periodo,          year(periodo) yearpart,         month(periodo) monthpart,         datepart(week, periodo) weekpart,         r, a, rz, b, c,          total int     @agenda ) select      yearpart, monthpart, weekpart,      sum(r) r, sum(a) a, sum(rz) rz, sum(b) b, sum(c) c,      sum(total) total mycte group yearpart, monthpart, weekpart 

hope helps :)


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -