SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    \m/\m/ karinne's Avatar
    Join Date
    Dec 2002
    Location
    Aylmer, QC, Canada
    Posts
    409
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    group by question

    one of my column type is datetime.

    how do i group by months or years?

    i tried

    select blog_date from blog group by month(blog_date) order by year(blog_date) asc, month(blog_date) asc

    but that doesn't seem to work?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    if you want to use ORDER BY in a query that also uses GROUP BY, then you must SELECT and GROUP BY every non-aggregate column that you want to ORDER BY
    Code:
    select year(blog_date)
         , month(blog_date)
      from blog 
    group 
        by year(blog_date)
         , month(blog_date) 
    order 
        by year(blog_date) asc
         , month(blog_date) asc
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •