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. ORDER BY in UNION

ORDER BY in UNION

Scheduled Pinned Locked Moved Database
databasesql-serversysadminquestion
14 Posts 6 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.
  • _ _Flaviu

    Is there possible to get an UNION in such a way that second part of UNION to be ordered ? I have:

    SELECT column_name(s) FROM table1
    UNION
    SELECT column_name(s) FROM table2 ORDER BY 3

    The select from table1 will always get one row, and I need to order just records that come from table2, which could be more than one row ... it is possible to achieve that by SQL ? P.S. I am using SQL Server.

    Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #5

    How about:

    SELECT column_name(s) FROM table1
    UNION
    SELECT column_name(s) FROM
    (SELECT column_name(s) FROM table2 ORDER BY 3)

    ?

    The difficult we do right away... ...the impossible takes slightly longer.

    _ 2 Replies Last reply
    0
    • _ _Flaviu

      Is there possible to get an UNION in such a way that second part of UNION to be ordered ? I have:

      SELECT column_name(s) FROM table1
      UNION
      SELECT column_name(s) FROM table2 ORDER BY 3

      The select from table1 will always get one row, and I need to order just records that come from table2, which could be more than one row ... it is possible to achieve that by SQL ? P.S. I am using SQL Server.

      D Offline
      D Offline
      David Mujica
      wrote on last edited by
      #6

      I know this is not going to be a popular opinion, but ... 1) Create a #TEMP table 2) Insert into the table with part 1 of the query 3) Insert into the table with part 2 (sorted) Return the dataset from the #TEMP table

      J K 2 Replies Last reply
      0
      • D David Mujica

        I know this is not going to be a popular opinion, but ... 1) Create a #TEMP table 2) Insert into the table with part 1 of the query 3) Insert into the table with part 2 (sorted) Return the dataset from the #TEMP table

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #7

        That is probably how I would do it. Although temp tables are also database specific the general idea and the layout of the solution works across different types of databases.

        1 Reply Last reply
        0
        • D David Mujica

          I know this is not going to be a popular opinion, but ... 1) Create a #TEMP table 2) Insert into the table with part 1 of the query 3) Insert into the table with part 2 (sorted) Return the dataset from the #TEMP table

          K Offline
          K Offline
          k5054
          wrote on last edited by
          #8

          Is there any guarantee that the temp table will be returned in the insertion order? If not, you'd have to add an identity column and return the temp table sorted on that.

          Keep Calm and Carry On

          J 1 Reply Last reply
          0
          • K k5054

            Is there any guarantee that the temp table will be returned in the insertion order? If not, you'd have to add an identity column and return the temp table sorted on that.

            Keep Calm and Carry On

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #9

            k5054 wrote:

            will be returned in the insertion order?

            Unlikely. And would not work anyways. Because you would do first one query then the second. So of course the temp table would need to provide a way to order it.

            1 Reply Last reply
            0
            • Richard Andrew x64R Richard Andrew x64

              How about:

              SELECT column_name(s) FROM table1
              UNION
              SELECT column_name(s) FROM
              (SELECT column_name(s) FROM table2 ORDER BY 3)

              ?

              The difficult we do right away... ...the impossible takes slightly longer.

              _ Offline
              _ Offline
              _Flaviu
              wrote on last edited by
              #10

              I have tried:

              SELECT a.id, a.name FROM my_table a WHERE a.id = 10416
              SELECT b.id, b.name FROM
              (SELECT b.id, b.name FROM b.my_table b WHERE b.parent_id = 10416)

              But it doesn't like it:

              Executing SQL directly; no cursor.
              Incorrect syntax near ')'.
              Statement(s) could not be prepared.

              Did I understand correctly your thought?

              1 Reply Last reply
              0
              • Richard Andrew x64R Richard Andrew x64

                How about:

                SELECT column_name(s) FROM table1
                UNION
                SELECT column_name(s) FROM
                (SELECT column_name(s) FROM table2 ORDER BY 3)

                ?

                The difficult we do right away... ...the impossible takes slightly longer.

                _ Offline
                _ Offline
                _Flaviu
                wrote on last edited by
                #11

                I have tried:

                SELECT a.id, a.name FROM my_table a WHERE a.id = 10416
                UNION
                SELECT b.id, b.name FROM
                (SELECT b.id, b.name FROM b.my_table b WHERE b.parent_id = 10416)

                But it doesn't like it:

                SQL
                Executing SQL directly; no cursor.
                Incorrect syntax near ')'.
                Statement(s) could not be prepared.

                Did I understand correctly your thought?

                Richard Andrew x64R 1 Reply Last reply
                0
                • _ _Flaviu

                  I have tried:

                  SELECT a.id, a.name FROM my_table a WHERE a.id = 10416
                  UNION
                  SELECT b.id, b.name FROM
                  (SELECT b.id, b.name FROM b.my_table b WHERE b.parent_id = 10416)

                  But it doesn't like it:

                  SQL
                  Executing SQL directly; no cursor.
                  Incorrect syntax near ')'.
                  Statement(s) could not be prepared.

                  Did I understand correctly your thought?

                  Richard Andrew x64R Offline
                  Richard Andrew x64R Offline
                  Richard Andrew x64
                  wrote on last edited by
                  #12

                  I'm sorry, try this slight modification:

                  SELECT a.id, a.name FROM my_table a WHERE a.id = 10416
                  UNION
                  SELECT S.id, S.name FROM
                  (SELECT b.id, b.name FROM b.my_table b WHERE b.parent_id = 10416) AS S

                  The difficult we do right away... ...the impossible takes slightly longer.

                  _ 1 Reply Last reply
                  0
                  • Richard Andrew x64R Richard Andrew x64

                    I'm sorry, try this slight modification:

                    SELECT a.id, a.name FROM my_table a WHERE a.id = 10416
                    UNION
                    SELECT S.id, S.name FROM
                    (SELECT b.id, b.name FROM b.my_table b WHERE b.parent_id = 10416) AS S

                    The difficult we do right away... ...the impossible takes slightly longer.

                    _ Offline
                    _ Offline
                    _Flaviu
                    wrote on last edited by
                    #13

                    Yes, that's worked:

                    SELECT a.id, a.name FROM my_table a WHERE a.id = 10416
                    UNION
                    SELECT S.id, S.name FROM
                    (SELECT b.id, b.name FROM b.my_table b WHERE b.parent_id = 10416) AS S

                    but soon as I put ORDER BY:

                    SELECT a.id, a.name FROM my_table a WHERE a.id = 10416
                    UNION
                    SELECT S.id, S.name FROM
                    (SELECT b.id, b.name FROM b.my_table b WHERE b.parent_id = 10416 ORDER BY 2) AS S

                    Error:

                    The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.

                    Richard Andrew x64R 1 Reply Last reply
                    0
                    • _ _Flaviu

                      Yes, that's worked:

                      SELECT a.id, a.name FROM my_table a WHERE a.id = 10416
                      UNION
                      SELECT S.id, S.name FROM
                      (SELECT b.id, b.name FROM b.my_table b WHERE b.parent_id = 10416) AS S

                      but soon as I put ORDER BY:

                      SELECT a.id, a.name FROM my_table a WHERE a.id = 10416
                      UNION
                      SELECT S.id, S.name FROM
                      (SELECT b.id, b.name FROM b.my_table b WHERE b.parent_id = 10416 ORDER BY 2) AS S

                      Error:

                      The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.

                      Richard Andrew x64R Offline
                      Richard Andrew x64R Offline
                      Richard Andrew x64
                      wrote on last edited by
                      #14

                      Whoops! I guess that's why nobody else recommended that. I forgot that ORDER BY is not allowed in derived tables. I think the temporary table is probably your best option.

                      The difficult we do right away... ...the impossible takes slightly longer.

                      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