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
 Assistance with Group BY Statement

Author  Topic 

meberg66219
Yak Posting Veteran

65 Posts

Posted - 2010-12-13 : 13:18:30
SELECT DATENAME(weekday, DateAdded) AS AddDate, DATEPART(week, DateAdded) AS WeekNumber,
DATEPART(year, DateAdded)AS YearNumber,
COUNT(SyStudentID) AS NewStudents
FROM dbo.SyStudent
WHERE
DateAdded >=dateadd(day,datediff(day,0,GetDate())- 7,0)
GROUP BY
DateAdded
ORDER BY
YearNumber,
WeekNumber,
AddDate

What I get as a return is:

AddDate=Friday
WeekNumber=50
YearNumber=2010
New Students=2

AddDate=Friday
WeekNumber=50
YearNumber=2010
NewStudents=1

and this goes on for however many entries there were for the day

What I need is this type of data:

AddDate=Friday
WeekNumber=50
YearNumber=2010
NewStudents=24

AddDate=Thursday
WeekNumber=50
YearNumber=2010
NewStudents=22

Etc.

I need the query to count the total number of students per day and report that back instead of the same day 25 times with a count total of 1 or 2.

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-12-13 : 13:36:14
[code]GROUP BY
DATENAME(weekday, DateAdded),
DATEPART(week, DateAdded),
DATEPART(year, DateAdded)[/code]
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-12-13 : 13:37:21
Try:
GROUP BY dateadd(day,datediff(day,0,DateAdded),0)
That will strip the time part in your group by.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

revdnrdy
Posting Yak Master

220 Posts

Posted - 2010-12-13 : 13:38:05
quote:
Originally posted by meberg66219

SELECT DATENAME(weekday, DateAdded) AS AddDate, DATEPART(week, DateAdded) AS WeekNumber, DATEPART(year, DateAdded)AS YearNumber,
COUNT(SyStudentID) AS NewStudents
FROM dbo.SyStudent
WHERE DateAdded >=dateadd(day,datediff(day,0,GetDate())- 7,0)
GROUP BY DateAdded
ORDER BY YearNumber, WeekNumber, AddDate

What I get as a return is:

AddDate=Friday
WeekNumber=50
YearNumber=2010
New Students=2

AddDate=Friday
WeekNumber=50
YearNumber=2010
NewStudents=1

and this goes on for however many entries there were for the day

What I need is this type of data:

AddDate=Friday
WeekNumber=50
YearNumber=2010
NewStudents=24

AddDate=Thursday
WeekNumber=50
YearNumber=2010
NewStudents=22

I need the query to count the total number of students per day and report that back instead of the same day 25 times with a count total of 1 or 2.



Would something like this work? I use it to count the number of help desk tickets created daily where I work. Perhaps you can modify it?

Select
count(DATENAME(weekday, DateAdded) AS AddDate),
DATEPART(week, DateAdded) AS WeekNumber,
DATEPART(year, DateAdded)AS YearNumber,
SyStudentID AS NewStudents

from dbo.SyStudent
group by AddDate
Go to Top of Page

meberg66219
Yak Posting Veteran

65 Posts

Posted - 2010-12-13 : 13:42:02
Thank you!!!! That worked perfectly!
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-12-13 : 13:44:01
Hm...
All given solutions?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-12-13 : 13:58:45
quote:
Originally posted by webfred

Hm...
All given solutions?


No, you're never too old to Yak'n'Roll if you're too young to die.

Yours and mine ought to anyway lol
Go to Top of Page

meberg66219
Yak Posting Veteran

65 Posts

Posted - 2010-12-14 : 13:33:50
Yes Russell and WebFred's solutions worked.
Go to Top of Page
   

- Advertisement -