Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to use ORDER by clause

How to use ORDER by clause

Scheduled Pinned Locked Moved Database
databasehelptutorial
3 Posts 2 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chris Meech
    wrote on last edited by
    #1

    Ordinarily I would create a view to solve this problem, but I'm unable to do that in this case because of parameters supplied to the WHERE clause. I have a query;

    select fielda,
    fieldb,
    fieldc
    from viewA
    where date_field between Date1 and Date2
    union
    select fielda,
    fieldb,
    fieldc,
    from viewB
    where date_field < Date2
    union ... 3 more similar select/union

    I need to have the resultant rows ordered in a particular sequence and so normally I would create a view, query the view and order the results of that query. In this instance, because of how the Date1 and Date2 are supplied, I can not use a view. Is there any way to ORDER the entire resultant set of rows when you use a union of multiple queries. Thanks. Chris

    C 1 Reply Last reply
    0
    • C Chris Meech

      Ordinarily I would create a view to solve this problem, but I'm unable to do that in this case because of parameters supplied to the WHERE clause. I have a query;

      select fielda,
      fieldb,
      fieldc
      from viewA
      where date_field between Date1 and Date2
      union
      select fielda,
      fieldb,
      fieldc,
      from viewB
      where date_field < Date2
      union ... 3 more similar select/union

      I need to have the resultant rows ordered in a particular sequence and so normally I would create a view, query the view and order the results of that query. In this instance, because of how the Date1 and Date2 are supplied, I can not use a view. Is there any way to ORDER the entire resultant set of rows when you use a union of multiple queries. Thanks. Chris

      C Offline
      C Offline
      Carlos Antollini
      wrote on last edited by
      #2

      Use a SubQuery. Select Field1, Field1 From (query with Unions) Order By Field1 Cheers!!!! Carlos Antollini.

      C 1 Reply Last reply
      0
      • C Carlos Antollini

        Use a SubQuery. Select Field1, Field1 From (query with Unions) Order By Field1 Cheers!!!! Carlos Antollini.

        C Offline
        C Offline
        Chris Meech
        wrote on last edited by
        #3

        Thanks for the response, Carlos. I'm using Oracle 8I and had tried to use that but continually got errors from the parser. Once I put the brace characters around the query with the unions, it worked like a champ. None of my books even gave an example such as yours. You helped me out a lot. Thanks again. Chris

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups