Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Group by and Date Issue

Author  Topic 

kathyc
Starting Member

9 Posts

Posted - 2009-03-14 : 12:19:14
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME

I have created the following query but I need to
display only those records where the createdon
date is between the StartDate and the EndDate.
How do I do this?

---------------------------------------------
SET @StartDate = '1/1/2009'
SET @EndDate = '12/12/2009'

SELECT
statecodename, statuscodename,createdon,
COUNT(statecodename) as GrandTotal
FROM dbo.FilteredLead
GROUP BY statecodename, statuscodename

HAVING createdon BETWEEN @startdate AND @enddate

ORDER BY statecodename;

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-03-14 : 12:43:56
[code]
SET @StartDate = '1/1/2009'
SET @EndDate = '12/12/2009'

SELECT
statecodename, statuscodename,
COUNT(statecodename) as GrandTotal
FROM dbo.FilteredLead
Where createdon BETWEEN @startdate AND @enddate
GROUP BY statecodename, statuscodename
ORDER BY statecodename;
[/code]
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-03-16 : 00:24:47
or

SELECT
statecodename, statuscodename,
COUNT(statecodename) as GrandTotal
FROM dbo.FilteredLead
Where createdon>=@startdate AND createdon<dateadd(day,1,@enddate)
GROUP BY statecodename, statuscodename
ORDER BY statecodename;

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -