Wednesday, March 7, 2012

Hours Days and Months

This code displays records by the the month they where recorded and displays the months as names (January...)
How can I do the Same for Days (Mon, Tues) and Hours (9.00,10.00)

Select DATENAME(mm,dDate) as Month, count(iStatID) as Total_Po
from ListingStats
Group by DATENAME(mm,dDate)
Order by DATENAME(mm,dDate)Weekly as well if possible|||Same way, Nate:

Select DATENAME(dw,date) as day, count(id) as Total_Po
from eventlog
Group by DATENAME(dw,date)
Order by DATENAME(dw,date)

Select DATENAME(wk, date) as weeknumber, count(id) as Total_Po
from eventlog
Group by DATENAME(wk,date)
Order by DATENAME(wk,date)

Select DATENAME(hh, date) as hour, count(id) as Total_Po
from eventlog
Group by DATENAME(hh,date)
Order by DATENAME(hh,date)

table and count column names were changed to protect the innoce...ok, because I was too lazy to change 'em to your names ;)

Look at BOL under the subject "DATEPART"|||THis Displays day of the Week

Select DATENAME(dw,dDate) as Month, count(iStatID) as Total_Po
from ListingStats
Group by DATENAME(dw,dDate)
Order by DATENAME(dw,dDate)|||Awesome thanks alot|||Not a problem! Remember always, BOL is our friend.|||What about joining Values Like
16-Sept

or Monday 16th

Can that be done with SQL? I'm trying to display the data in crystal reports.|||You bet!

Take a look at Books Online in CONVERT and STRING system functions.|||I think Ive figured them out thanks for your help need some more test data can't be bothered loading it right now.

Im pretty sure this loads the last seven days views

Select DATENAME(dd,dDate)+ ' ' + Substring(DATENAME(mm,dDate),0,4) as Day, count(iStatID) as Views
from ListingStats Where IListingID = @.ListingID AND (dDate < GetDate()) AND (dDate > DATENAME(dd,GetDate())-7)
Group by DATENAME(dd,dDate)+ ' ' + Substring(DATENAME(mm,dDate),0,4)
Order by DATENAME(dd,dDate)+ ' ' + Substring(DATENAME(mm,dDate),0,4)

Thanks for your help much appreciated

No comments:

Post a Comment