c# – 每日/每周/每月记录计数通过StoredProcedure搜索

使用MS SQL Server.I已经创建了一个名为SP_Get_CallsLogged的存储过程.

我有一个名为TRN_Call的表,它有一个名为CallTime的列,它是一个DateTime.

我的应用程序中有一个用户输入的网页: –

> StartDate(DateTime)
> EndDate(DateTime)
>期间(每日/每周/每月)(varchar)(来自DropDownList)

我希望根据用户在DropDownList中选择的指定时段(每日/每周/每月),在我的表TRN_Call中获取这些调用的记录计数.

例如

StartDate ='1/18/2010 11:10:46 AM'
EndDate   ='1/25/2010 01:10:46 AM'
Period    =Daily

因此,上述日期(StartDate EndDate)之间的记录计数应该以某种方式进行,以便我可以单独参考这些计数,即: –

Date 1/18/2010 Records Found 5
Date 1/19/2010 Records Found 50
Date 1/20/2010 Records Found 15
Date 1/21/2010 Records Found 32
Date 1/22/2010 Records Found 12
Date 1/23/2010 Records Found 15
Date 1/24/2010 Records Found 17
Date 1/25/2010 Records Found 32

并将这些计数发送到Web应用程序,以便可以在Crystal Reports中列出这些计数.

最佳答案 我将编辑您的存储过程以传递在您的网页中选择的开始日期和结束日期,以便您可以将它们包含在WHERE子句中.

-- Daily
SELECT CallTime, COUNT(*) AS 'Records Found'
FROM TRN_Call
WHERE CallTime BETWEEN @StartDate AND @EndDate
GROUP BY CallTime

每周可能相当复杂.

如果您查看此link,您将看到一个功能(如下所示),您可以使用该功能来帮助您获得每周结果

例如所以创建下面的功能

create function FiscalWeek (@startMonth varchar(2), @myDate datetime)  
returns int  
as  
begin  
declare @firstWeek datetime  
declare @weekNum int  
declare @year int  
set @year = datepart(year, @myDate)+1  
--Get 4th day of month of next year, this will always be in week 1  
set @firstWeek = convert(datetime, str(@year)+@startMonth+'04', 102)  
--Retreat to beginning of week  
set @firstWeek = dateadd(day, (1-datepart(dw, @firstWeek)), @firstWeek)  
while @myDate < @firstWeek --Repeat the above steps but for previous year  
 begin  
  set @year = @year - 1  
  set @firstWeek = convert(datetime, str(@year)+@startMonth+'04', 102)  
  set @firstWeek = dateadd(day, (1-datepart(dw, @firstWeek)), @firstWeek)  
 end  
set @weekNum = (@year*100)+((datediff(day, @firstweek, @myDate)/7)+1)  
return @weekNum  
end  

现在,您应该能够运行下面的代码,该代码应该按周分组.要在日,周或年之间进行选择,您还必须将用户选择的期间传递给存储过程.

(ps.还没有完成一年的工作)

SELECT dbo.FiscalWeek('04', CallTime), dbo.FiscalWeek('04', CallTime)%100 ,  COUNT(*) AS 'Records Found - Weekly'
FROM TRN_Call
WHERE CallTime BETWEEN @StartDate AND @EndDate
GROUP BY dbo.FiscalWeek('04', CallTime), dbo.FiscalWeek('04', CallTime)%100  
点赞