ORDER BY in UNION
-
Is there possible to get an
UNION
in such a way that second part ofUNION
to be ordered ? I have:SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2 ORDER BY 3The select from
table1
will always get one row, and I need to order just records that come fromtable2
, which could be more than one row ... it is possible to achieve that by SQL ? P.S. I am using SQL Server.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.
-
Is there possible to get an
UNION
in such a way that second part ofUNION
to be ordered ? I have:SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2 ORDER BY 3The select from
table1
will always get one row, and I need to order just records that come fromtable2
, which could be more than one row ... it is possible to achieve that by SQL ? P.S. I am using SQL Server.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
-
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
-
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
-
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
-
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.
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?
-
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.
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?
-
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?
-
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 Sbut 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 SError:
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.
-
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 Sbut 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 SError:
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.
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.