+ Reply to Thread
Results 1 to 10 of 10

Thread: order by group by problem

  1. #1
    Barn Loyal todd2006 is an unknown quantity at this point todd2006's Avatar
    Join Date
    Mar 2008
    Posts
    889
    Rep Power
    4

    order by group by problem

    Here is my code I am having problem with the order by clause

    Code:
    sqlstr="SELECT Distinct MONTH(Reg_Date) AS Expr1, YEAR(Reg_Date) AS Expr2 FROM Registration  GROUP BY Reg_Date"
    
    When it displays the results they are like this

    1/2008
    3/2008
    2/2009
    4/2008
    3/2009

    the records are not ordered properly I need them to be ordered in desc manner

    can someone tell me how I can do it

    todd

  2. #2
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    i cant see any "order by" clause in ur sql

  3. #3
    Barn Loyal todd2006 is an unknown quantity at this point todd2006's Avatar
    Join Date
    Mar 2008
    Posts
    889
    Rep Power
    4

    Micky,

    I tried the order by clause before group by

    sqlstr="SELECT Distinct MONTH(Reg_Date) AS Expr1, YEAR(Reg_Date) AS Expr2 FROM Registration order by Reg_Date desc GROUP BY Reg_Date"


    its not working

  4. #4
    Super Sarcasm Mistress mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere's Avatar
    Join Date
    Mar 2008
    Location
    Wide Awake In Dreamland
    Posts
    830
    Rep Power
    8

    The ORDER BY clause should be after your GROUP BY clause.
    Quote of the Month:
    INSIGHT: When the going gets tough, the tough get going. The smart left a long time ago.

    Questions to Ponder:
    Are people more violently opposed to fur rather than leather because it's much easier to harass rich women than motorcycle gangs?

    iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
    copyright © 2008 sbenj69

    Sarchasm: The gulf between the author of sarcastic wit and the person who doesn't get it.

  5. #5
    Moderator don94403 is a jewel in the rough don94403 is a jewel in the rough don94403 is a jewel in the rough don94403's Avatar
    Join Date
    Mar 2008
    Location
    San Mateo, CA, USA
    Posts
    313
    Blog Entries
    8
    Real Name
    Don Ravey
    Rep Power
    6

    Micky and Mehere are correct. ORDER BY and GROUP BY do entirely different things. You need to understand what each does and when you use one, the other, or both.

  6. #6
    Barn Loyal todd2006 is an unknown quantity at this point todd2006's Avatar
    Join Date
    Mar 2008
    Posts
    889
    Rep Power
    4

    Ok this is what I am doing.

    I want to display the distinct month/year in a drop down and those should be ordered in desc

    then when the admin clicks on the month/year he will see all the students registered for that specific month and year.


    right now with my query it does the distinct month/year but the order by clause is not working.

    I tried to put the order by clause after group by and it still didnt work.

    any idea

  7. #7
    Super Sarcasm Mistress mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere is a glorious beacon of light mehere's Avatar
    Join Date
    Mar 2008
    Location
    Wide Awake In Dreamland
    Posts
    830
    Rep Power
    8

    try this:
    Code:
    sqlstr="SELECT Distinct MONTH(Reg_Date) AS month_date, YEAR(Reg_Date) AS year_date FROM Registration ORDER BY CInt(YEAR(Reg_Date)) desc, CInt(MONTH(Reg_Date)) desc"
    
    and you need to tell us more than "it didn't work". this doesn't help us know what the issue is.
    Quote of the Month:
    INSIGHT: When the going gets tough, the tough get going. The smart left a long time ago.

    Questions to Ponder:
    Are people more violently opposed to fur rather than leather because it's much easier to harass rich women than motorcycle gangs?

    iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
    copyright © 2008 sbenj69

    Sarchasm: The gulf between the author of sarcastic wit and the person who doesn't get it.

  8. #8
    Barn Loyal todd2006 is an unknown quantity at this point todd2006's Avatar
    Join Date
    Mar 2008
    Posts
    889
    Rep Power
    4

    sorry abt that mehere it gives an error

    ORDER BY clause (CInt(YEAR(Reg_Date))) conflicts with DISTINCT.

  9. #9
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Try the following query. GROUP BY and DISTINCT oft times do the same thing.
    Code:
    SELECT MONTH(Reg_Date) AS Expr1
         , YEAR(Reg_Date) AS Expr2 
      FROM Registration 
    GROUP BY YEAR(Reg_Date), MONTH(Reg_Date)
    ORDER BY YEAR(Reg_Date) desc , MONTH(Reg_Date) desc
    
    I admit to not trying this with sample data, but it does parse.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  10. #10
    Barn Loyal todd2006 is an unknown quantity at this point todd2006's Avatar
    Join Date
    Mar 2008
    Posts
    889
    Rep Power
    4

    yes it worked out thank you so much.

+ Reply to Thread

Similar Threads

  1. Order by
    By todd2006 in forum SQL Development
    Replies: 12
    Last Post: February 25th, 2009, 03:12 PM
  2. Sql group by
    By BLaaaaaaaaaarche in forum SQL Development
    Replies: 4
    Last Post: February 12th, 2009, 12:27 PM
  3. order by row1 + row2
    By dr_rock in forum Microsoft SQL Server
    Replies: 2
    Last Post: February 2nd, 2009, 11:41 AM
  4. Summing for each group only
    By Rebelle in forum ASP Development
    Replies: 13
    Last Post: June 5th, 2008, 02:22 PM
  5. How can I randomly order the contents of an array?
    By theChris in forum PHP Development
    Replies: 1
    Last Post: May 14th, 2008, 11:09 AM

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

SEO by vBSEO