Showing posts with label calldate. Show all posts
Showing posts with label calldate. Show all posts

Monday, March 26, 2012

Grouping by week in stored procedure

Hi all,

I am using the below statement to get some dates grouped by date, in my SP.

SELECT TOP 100 PERCENT COUNT(dbo.test.CallDate) AS CallCount, year(dbo.test.CallDate) AS CallYear, datepart(wk, dbo.test.CallDate) AS [Week]
FROM dbo.test

LEFT OUTER JOIN dbo.view1 ON dbo.test.CallID = dbo.view1.CallID
LEFT OUTER JOIN dbo.view2 ON dbo.test.CallID = dbo.view2.CallID

WHERE (dbo.view1.[ACCOUNT ID] = @.accountid

OR (dbo.view2.[ACCOUNT ID] = @.accountid

AND (convert(varchar(10),dbo.test.CallDate,121) BETWEEN CONVERT(DATETIME, @.StartDate, 102)AND CONVERT(DATETIME, @.EndDate, 102))
GROUP BY year(dbo.test.CallDate), datepart(wk, dbo.test.CallDate)
ORDER BY year(dbo.test.CallDate), datepart(wk, dbo.test.CallDate)

i gave startdate as 1/1/2007 and endDate as 2/18/2007

i am getting the reuslt as
count year week
42 2001 32
2 2001 39
1 2001 51
1 2002 17
1 2002 19
106 2002 21
183 2002 22
226 2002 23
.........................
...........................
1208 2007 1
1292 2007 2

actually i should get only the last 2 rows.
Can anyone please point out, why i am getting the 2001, 2002 data? and how to fix that?

Thanks

Looks to me like you've got your bracketing wrong round your OR clause...

It's currently WHERE ( A OR (B AND C))

Shouldn't it be WHERE (A OR B) AND C?

|||

Thanks

Great help

It worked

Friday, March 9, 2012

GROUP BY Query

Hi all! Please help me! Suppose I have this in table
CallDate
9/17/2005
9/16/2005
8/20/2005
8/14/2005
8/11/2005
7/23/2005
and I want to query the table to have this result:
Sep 2005 2
Aug 2005 3
Jul 2005 1
Thanks!SELECT SUBSTRING(CONVERT(VARCHAR,MIN(calldate),113),4,8) AS mth,
COUNT(*) AS cnt
FROM YourTable
GROUP BY DATEDIFF(MONTH,'20000101',calldate) ;
--
David Portas
SQL Server MVP
--|||Hi
Maybe:
SELECT MONTH(calldate),YEAR(calldate),COUNT(*) as CNT
FROM MyTable
GROUP BY MONTH(calldate),YEAR(calldate)
OR
SELECT RIGHT(CONVERT(CHAR(11),calldate,113),8) as CallMonthYear,COUNT(*) as
CNT
FROM MyTable
GROUP BY RIGHT(CONVERT(CHAR(11),calldate,113),8)
John
"Stanley" <xstanley@.gmail.com> wrote in message
news:1126953269.031997.92620@.g14g2000cwa.googlegroups.com...
> Hi all! Please help me! Suppose I have this in table
> CallDate
> 9/17/2005
> 9/16/2005
> 8/20/2005
> 8/14/2005
> 8/11/2005
> 7/23/2005
> and I want to query the table to have this result:
> Sep 2005 2
> Aug 2005 3
> Jul 2005 1
> Thanks!
>|||SELECT COUNT(*),DATENAME(MONTH,CALLDATE)+' ' +
LTRIM(STR(DATENAME(YEAR,CALLDATE)))
FROM MYTABLE
GROUP BY DATENAME(MONTH,CALLDATE)+' ' +
LTRIM(STR(DATENAME(YEAR,CALLDATE)))
"Stanley" <xstanley@.gmail.com> wrote in message
news:1126953269.031997.92620@.g14g2000cwa.googlegroups.com...
> Hi all! Please help me! Suppose I have this in table
> CallDate
> 9/17/2005
> 9/16/2005
> 8/20/2005
> 8/14/2005
> 8/11/2005
> 7/23/2005
> and I want to query the table to have this result:
> Sep 2005 2
> Aug 2005 3
> Jul 2005 1
> Thanks!
>|||Thanks for all replies! You all are helpful people! Thanks