关于我之前发布的这个问题,我有一个问题: -
Daily/Weekly/Monthly Record Count Search via StoredProcedure
我希望以每周为基础和每月基础,每日基础问题得到解决。
问题编号@ 1:每周呼叫计数以及开始日期和周结束日期
我搜索了周的开始日期和结束日期,包括他们各自的呼叫计数以及下面提到的查询。但问题是我无法在单个表中得到结果,尽管我已经使用了临时表(#TempTable +#TempTable2)。请在这方面帮助我。
注意:表创建被注释为执行多次。
--CREATE TABLE #TempTable(StartDate datetime,EndDate datetime,CallCount numeric(18,5))
--CREATE TABLE #TempTable2(StartDate datetime,EndDate datetime,CallCount numeric(18,5))
DECLARE @StartDate datetime,@EndDate datetime,@StartDateTemp1 datetime,@StartDateTemp2 datetime,@EndDateTemp datetime,@Period varchar(50);
SET @StartDate='1/1/2010'; SET @EndDate='2/28/2010';
SET @StartDateTemp1=@StartDate; SET @StartDateTemp2=DATEADD(dd, 7, @StartDate );
SET @Period='Weekly';
IF (@Period = 'Weekly')
BEGIN
WHILE ((@StartDate <= @StartDateTemp1) AND (@StartDateTemp2 <= @EndDate))
BEGIN
IF((@StartDateTemp1 < @StartDateTemp2 ) AND (@StartDateTemp1 != @StartDateTemp2) )
BEGIN
SELECT
convert(varchar, @StartDateTemp1, 106) AS 'Start Date',
convert(varchar, @StartDateTemp2, 106) AS 'End Date',
COUNT(*) AS 'Call Count'
FROM TRN_Call
WHERE (CallTime >= @StartDateTemp1 AND CallTime <= @StartDateTemp2 );
END
SET @StartDateTemp1 = DATEADD(dd, 7, @StartDateTemp1);
SET @StartDateTemp2 = DATEADD(dd, 7, @StartDateTemp2);
END
END
问题编号@ 2:每月基于呼叫的计数以及开始日期和周结束日期 在这种情况下,我有相同的搜索,但必须搜索呼叫计数加上月的开始日期和结束日期。在这方面也请帮助我。
DECLARE @StartDate datetime,@EndDate datetime,@StartDateTemp1 datetime,@StartDateTemp2 datetime,@EndDateTemp datetime,@Period varchar(50);
SET @StartDate='1/1/2010'; SET @EndDate='4/1/2010'; SET @StartDateTemp1=@StartDate;
--SET @StartDateTemp2=@StartDate;
SET @StartDateTemp2=DATEADD(mm, 1, @StartDate );
SET @Period='Monthly';
IF (@Period = 'Monthly')
BEGIN
WHILE ((@StartDate <= @StartDateTemp1) AND (@StartDateTemp2 <= @EndDate))
BEGIN
IF((@StartDateTemp1 < @StartDateTemp2 ) AND (@StartDateTemp1 != @StartDateTemp2) )
BEGIN
SELECT
convert(varchar, @StartDateTemp1, 106) AS 'Start Date',
convert(varchar, @StartDateTemp2, 106) AS 'End Date',
COUNT(*) AS 'Call Count'
FROM TRN_Call
WHERE (CallTime >= @StartDateTemp1 AND CallTime <= @StartDateTemp2 );
END
SET @StartDateTemp1 = DATEADD(mm, 1, @StartDateTemp1);
SET @StartDateTemp2 = DATEADD(mm, 1, @StartDateTemp2);
END
END
答案 0 :(得分:1)
我相信以下三个简单的查询足以满足您的需求。
每日强>
SELECT [Day] = CAST(CAST(CallTime AS INTEGER) AS DATETIME)
, [Call Count] = COUNT(*)
FROM TRN_Call
WHERE CallTime BETWEEN @StartDate AND @EndDate
GROUP BY CAST(CAST(CallTime AS INTEGER) AS DATETIME)
<强>每周强>
SELECT [Week] = DATEPART(ww, CallTime)
, [Year] = DATEPART(yy, CallTime)
, [Call Count] = COUNT(*)
FROM TRN_Call
WHERE CallTime BETWEEN @StartDate AND @EndDate
GROUP BY DATEPART(ww, CallTime), DATEPART(yy, CallTime)
<强>每月强>
SELECT [Month] = DATEPART(mm, CallTime)
, [Year] = DATEPART(yy, CallTime)
, [Call Count] = COUNT(*)
FROM TRN_Call
WHERE CallTime BETWEEN @StartDate AND @EndDate
GROUP BY DATEPART(mm, CallTime), DATEPART(yy, CallTime)
答案 1 :(得分:0)
简单查询查找记录月份明智.....
SELECT invoice_no,created_at,month(created_at)month_no,count(invoice_no)sale_count FROM sales_invoice
WHERE company_id
=&#39; .10。 &#39; AND status
=&#34;已批准&#34; and year(created_at)= year(curdate())group by month(created_at)enter code here