• SQL (Access 2007)

    Author
    Topic
    #450101

    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?

    Viewing 0 reply threads
    Author
    Replies
    • #1105126

      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

    Viewing 0 reply threads
    Reply To: SQL (Access 2007)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: