views:

193

answers:

1

I have a question in connection to this question earlier posted by me:-

http://stackoverflow.com/questions/2452984/daily-weekly-monthly-record-count-search-via-storedprocedure

I want to have the Count of Calls on Weekly-basis and Monthly-basis, Daily-basis issue is resolved.

ISSUE NUMBER @1:Weekly-basis Count of Calls and Start-Date and End-Date of Week

I have searched the Start-Date and End-Date of Week including their individual Count of Calls as well in the below-mentioned query. But the problem is that I could not get the result in one single table, although I have used the Temporary Tables(#TempTable+#TempTable2). Kindly help me in this regards.

NOTE:Table Creation commented as for executing more than once.

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

ISSUE NUMBER @2:Monthly-basis Count of Calls and Start-Date and End-Date of Week In this case, I have the same search, but will have to search the Call Counts plus the Start-Date and End-Date of the Month. Kindly help me in this regards as well.

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

I believe following three simple queries suffice for what you need.

Daily

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)

Weekly

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)

Monthly

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)
Lieven
@Lieven: I've done this previously what you have answered, but my requirement is not just to search the Weekly or Monthly Count.I have to show the Start-Date and End-Date(including the Count) for a specific range.Week:-Start-Date End-Date Count15 Jan 2010 22 Jan 2010 2122 Jan 2010 29 Jan 2010 73These above results are coming in separate results, I want to combine them and put in single result. I mean, by my query these results are coming 1-by-1 in different result windows.Monthly:-Start-Date End-Date Count01 Jan 2010 01 Feb 2010 11801 Feb 2010 01 Mar 2010 84
Muzaffar Ali Rana