Results 1 to 3 of 3
  1. #1
    4 Star Lounger
    Join Date
    Apr 2001
    Location
    Guatemala City
    Posts
    515
    Thanks
    0
    Thanked 0 Times in 0 Posts

    SQL (Access 2007)

    I am trying to learn SQL, but am having trouble with a relatively simple SELECT statement, as follows:

    use northwind
    select orderid, sum(quantity*unitprice-discount) as Monto
    from [order details]
    --order by (sum(quantity*unitprice-discount))
    group by orderid

    As it stands, the statement works; however, I want to order the listing in descending order by the calculated field. When I take out the comments indication, I get::

    Server: Msg 156, Level 15, State 1, Line 9
    Incorrect syntax near the keyword 'group'.

    Could you help me?

  2. #2
    4 Star Lounger
    Join Date
    Apr 2001
    Location
    Guatemala City
    Posts
    515
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: SQL (Access 2007)

    Thank you.

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: SQL (Access 2007)

    ORDER BY is always the last part of a SELECT statement, so ORDER BY should come after GROUP BY. To sort descending, add DESC after the field/expression in the ORDER BY part.

    use northwind
    select orderid, sum(quantity*unitprice-discount) as Monto
    from [order details]
    group by orderid
    order by (sum(quantity*unitprice-discount)) desc

Posting Permissions

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