GROUP BY and dates in SQL (again)

This is a discussion on "GROUP BY and dates in SQL (again)" within the Databases section. This forum, and the thread "GROUP BY and dates in SQL (again) are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > Databases

Notices


Closed Thread
 
LinkBack (1) Thread Tools
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old Dec 13th, 2004, 14:32
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 23
Posts: 1,651
Blog Entries: 1
Thanks: 0
Thanked 4 Times in 4 Posts
GROUP BY and dates in SQL (again)

trying to using GROUP BY statement on a SQL Server DB.
GROUP BY [date] groups by date and time.
Tried using the CONVERT function but no success...

any tips?
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)

  #2 (permalink)  
Old Dec 13th, 2004, 14:43
Reputable Member
Join Date: Aug 2003
Location: United Kingdom
Posts: 341
Thanks: 0
Thanked 0 Times in 0 Posts
tried DATEPART()?
  #3 (permalink)  
Old Dec 13th, 2004, 16:34
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 23
Posts: 1,651
Blog Entries: 1
Thanks: 0
Thanked 4 Times in 4 Posts
this is what I've already tried:

CONVERT(DATETIME, [date], 5) ' 5: dd-mm-yy
result: still returns time

CONVERT(DATETIME, [date], 6) ' 6: dd mmm yy
result: still returns time

CONVERT(CHAR(12), [date], 5) ' 5: dd-mm-yy
result: returns 'dd-mm-yy' but doesn't sort as a date, sorts alphabetically instead
i.e.: date between 28-nov and 02-dec shows up in this order:
01 dec 04
02 dec 04
28 nov 04
29 nov 04
30 nov 04


I'm about to try datepart... I'll let you know
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
  #4 (permalink)  
Old Dec 13th, 2004, 16:56
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 23
Posts: 1,651
Blog Entries: 1
Thanks: 0
Thanked 4 Times in 4 Posts
great, I think I'm nearly there....

this worked:
DatePart(dt, [date]) ' day of year

but now I have to somehow display the actual dates because this makes much more sense:
30 orders on nov-27


than this:
30 orders on day 317
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
  #5 (permalink)  
Old Dec 13th, 2004, 17:26
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 23
Posts: 1,651
Blog Entries: 1
Thanks: 0
Thanked 4 Times in 4 Posts
GOT IT!

I used DATEPART to group by year and day and used CONVERT to return the date I want to display as a string. then I sort by year and day.........

I'll post it in case somebody else comes across the same problem:

Code: Select all
SELECT DATEPART(yy, Orders.[date]) AS [y],
       DATEPART(dy, Orders.[date]) AS [d], 
       CONVERT(CHAR(12), Orders.[date], 106) AS [date],
       Count(Orders.[order]) AS n
FROM
       [....]
WHERE
       [....]
GROUP BY
       DATEPART(yy, Orders.[date]),
       DATEPART(dy, Orders.[date]),
       CONVERT(CHAR(12), Orders.[date], 106)
ORDER BY
       DATEPART(yy, Orders.[date]),
       DATEPART(dy, Orders.[date])
this site was very helpfull:
http://manuals.sybase.com/onlinebook...y=convert+date
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Closed Thread

Tags
group, dates, sql, again

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.webforumz.com/databases/905-group-by-and-dates-in-sql.htm
Posted By For Type Date
Yamini's bookmarks tagged with This thread Refback Nov 9th, 2006 15:29

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP dates simonb PHP Forum 11 Sep 5th, 2007 14:30
Days between 2 Dates birdy247 JavaScript Forum 1 Apr 19th, 2007 19:55
dates in SQL spinal007 Databases 3 Nov 10th, 2004 09:32
dates benbacardi Classic ASP 2 Sep 14th, 2004 16:18
New to the Group pahprints Introduce Yourself 8 Jun 15th, 2004 21:52


All times are GMT. The time now is 09:51.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43