Order table by two columns The 2019 Stack Overflow Developer Survey Results Are InSequential joining of tables in orderGroup by + Order by Behavior Explanation?Best way to rank all the columns in a table and store those ranks in another tableSet in mySQL a predefined order for SELECT queries without using the ORDER BY clauseHow do I control order/arrangement of certain columns in a resultset?Transposing hierachical data from one VARCHAR to two INTsHow can I speed up an ASC sort on a column that only holds an integer between 0 and 9 across multiple millions of rows?Mysql: slow down with order by, global database problemORDER BY doesn't use an index on SQL Server if CASE WHEN is presentHow to identify columns order in a table, bis

Can an undergraduate be advised by a professor who is very far away?

Old scifi movie from the 50s or 60s with men in solid red uniforms who interrogate a spy from the past

How do I free up internal storage if I don't have any apps downloaded?

Why does the nucleus not repel itself?

What does もの mean in this sentence?

What to do when moving next to a bird sanctuary with a loosely-domesticated cat?

What is this business jet?

Loose spokes after only a few rides

The phrase "to the numbers born"?

What information about me do stores get via my credit card?

Cooking pasta in a water boiler

Accepted by European university, rejected by all American ones I applied to? Possible reasons?

Why not take a picture of a closer black hole?

What does Linus Torvalds mean when he says that Git "never ever" tracks a file?

Why doesn't shell automatically fix "useless use of cat"?

Can a flute soloist sit?

Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?

Is it safe to harvest rainwater that fell on solar panels?

What could be the right powersource for 15 seconds lifespan disposable giant chainsaw?

Likelihood that a superbug or lethal virus could come from a landfill

How to charge AirPods to keep battery healthy?

For what reasons would an animal species NOT cross a *horizontal* land bridge?

What is preventing me from simply constructing a hash that's lower than the current target?

Match Roman Numerals



Order table by two columns



The 2019 Stack Overflow Developer Survey Results Are InSequential joining of tables in orderGroup by + Order by Behavior Explanation?Best way to rank all the columns in a table and store those ranks in another tableSet in mySQL a predefined order for SELECT queries without using the ORDER BY clauseHow do I control order/arrangement of certain columns in a resultset?Transposing hierachical data from one VARCHAR to two INTsHow can I speed up an ASC sort on a column that only holds an integer between 0 and 9 across multiple millions of rows?Mysql: slow down with order by, global database problemORDER BY doesn't use an index on SQL Server if CASE WHEN is presentHow to identify columns order in a table, bis



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















I would like to order this table but I can not find a way to make it work. Can you help me?



I have this table:



The table



I need this:



The other one



I have a table where documents are listed, the first field is the id of the document and the second field represents the father, so I must show a list where you can see in an orderly manner that 242 is the first document and 252 and 335 were generated from the 242.



The query:



select * from table_name order by col1 ASC, Col2 ASC


...doesn't work for me. I do this query:



SELECT
FR1.[report_id],
FR1.[report_parent]
FROM [FARA_reports] FR1
WHERE
FR1.[report_is_delete] <> 1
AND FR1.[report_is_tmp] <> 1
ORDER BY
FR1.[report_id] asc,
FR1.[report_parent] desc


and this is my first image.










share|improve this question









New contributor




Liliana del Carmen Castellanos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


























    3















    I would like to order this table but I can not find a way to make it work. Can you help me?



    I have this table:



    The table



    I need this:



    The other one



    I have a table where documents are listed, the first field is the id of the document and the second field represents the father, so I must show a list where you can see in an orderly manner that 242 is the first document and 252 and 335 were generated from the 242.



    The query:



    select * from table_name order by col1 ASC, Col2 ASC


    ...doesn't work for me. I do this query:



    SELECT
    FR1.[report_id],
    FR1.[report_parent]
    FROM [FARA_reports] FR1
    WHERE
    FR1.[report_is_delete] <> 1
    AND FR1.[report_is_tmp] <> 1
    ORDER BY
    FR1.[report_id] asc,
    FR1.[report_parent] desc


    and this is my first image.










    share|improve this question









    New contributor




    Liliana del Carmen Castellanos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      3












      3








      3


      0






      I would like to order this table but I can not find a way to make it work. Can you help me?



      I have this table:



      The table



      I need this:



      The other one



      I have a table where documents are listed, the first field is the id of the document and the second field represents the father, so I must show a list where you can see in an orderly manner that 242 is the first document and 252 and 335 were generated from the 242.



      The query:



      select * from table_name order by col1 ASC, Col2 ASC


      ...doesn't work for me. I do this query:



      SELECT
      FR1.[report_id],
      FR1.[report_parent]
      FROM [FARA_reports] FR1
      WHERE
      FR1.[report_is_delete] <> 1
      AND FR1.[report_is_tmp] <> 1
      ORDER BY
      FR1.[report_id] asc,
      FR1.[report_parent] desc


      and this is my first image.










      share|improve this question









      New contributor




      Liliana del Carmen Castellanos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I would like to order this table but I can not find a way to make it work. Can you help me?



      I have this table:



      The table



      I need this:



      The other one



      I have a table where documents are listed, the first field is the id of the document and the second field represents the father, so I must show a list where you can see in an orderly manner that 242 is the first document and 252 and 335 were generated from the 242.



      The query:



      select * from table_name order by col1 ASC, Col2 ASC


      ...doesn't work for me. I do this query:



      SELECT
      FR1.[report_id],
      FR1.[report_parent]
      FROM [FARA_reports] FR1
      WHERE
      FR1.[report_is_delete] <> 1
      AND FR1.[report_is_tmp] <> 1
      ORDER BY
      FR1.[report_id] asc,
      FR1.[report_parent] desc


      and this is my first image.







      sql-server sql-server-2008 order-by






      share|improve this question









      New contributor




      Liliana del Carmen Castellanos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Liliana del Carmen Castellanos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited yesterday









      Paul White

      54.2k14288461




      54.2k14288461






      New contributor




      Liliana del Carmen Castellanos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 days ago









      Liliana del Carmen CastellanosLiliana del Carmen Castellanos

      182




      182




      New contributor




      Liliana del Carmen Castellanos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Liliana del Carmen Castellanos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Liliana del Carmen Castellanos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          5 Answers
          5






          active

          oldest

          votes


















          5














          Another option:



          select a, b
          from t
          order by
          case when a < b or b is null then a else b end,
          case when a < b or b is null then b else a end ;


          Test in dbfiddle.uk.






          share|improve this answer























          • Can't you just order by isnull(id2, id1), id2?

            – bendataclear
            yesterday






          • 1





            @bendtaclear yes. When I wrote the answer, it wasn't clear that first column can never be null.

            – ypercubeᵀᴹ
            yesterday











          • These really work for me :D

            – Liliana del Carmen Castellanos
            yesterday


















          8














          Your data:



          CREATE TABLE dbo.WEIRD_ORDERING
          (
          ID1 INT NULL,
          ID2 INT NULL
          );

          INSERT INTO dbo.WEIRD_ORDERING VALUES
          (242, NULL),
          (243, NULL),
          (244, NULL),
          (252, 242),
          (254, NULL),
          (255, NULL),
          (256, NULL),
          (292, NULL),
          (308, NULL),
          (311, NULL),
          (313, 311),
          (314, 311),
          (323, NULL),
          (324, 311),
          (335, 242),
          (340, NULL),
          (341, NULL),
          (358, NULL),
          (372, NULL),
          (373, NULL),
          (377, NULL),
          (378, 358),
          (379, 358),
          (380, 358),
          (381, 358);


          This gives you results in the desired order:



          SELECT ID1, ID2
          FROM
          (
          SELECT ID1, ID2, ID1 AS ORDERING_COLUMN
          FROM dbo.WEIRD_ORDERING
          WHERE ID1 <= ID2 OR ID2 IS NULL

          UNION ALL

          SELECT ID1, ID2, ID2 AS ORDERING_COLUMN
          FROM dbo.WEIRD_ORDERING
          WHERE ID1 > ID2 OR ID1 IS NULL
          ) q
          ORDER BY ORDERING_COLUMN;


          db fiddle






          share|improve this answer























          • Thank you so much. You are AWESOME!! :D

            – Liliana del Carmen Castellanos
            yesterday











          • This doesn't quite work, if you reorder your input data from (313, 311), (314, 311) to (314, 311),(313, 311) it comes out in the wrong order, I suggest updating the order by statement to be ORDER BY ORDERING_COLUMN, ID1 to fix that

            – Xynos
            yesterday











          • @Xynos feel free to edit

            – Joe Obbish
            yesterday


















          3














          Stealing the sql fiddle table from Joe Obbish this is a simple solution



          SELECT * FROM dbo.WEIRD_ORDERING
          ORDER BY coalesce(ID2*1000+1, ID1*1000)





          share|improve this answer








          New contributor




          Spaceben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.



























            2














            SELECT *
            FROM datatable
            ORDER BY CASE WHEN field2 IS NULL THEN field1 ELSE field2 END, -- sort groups
            CASE WHEN field2 IS NULL THEN 1 ELSE 2 END, -- move parent first
            field2 -- sort childs





            share|improve this answer






























              2














              All answers so far rely on an assumption that there are no further levels of "generated from" (e.g. there is no 382 generated from the 381 which is itself generated from 352 - or which was it).



              They should know what is said of "assume", so they should have asked you about this first.



              If you can indeed have further levels of "generated from", then you need a recursive query to do the full path construction ("352" , "352/381" , "352/381/382" , etc etc) and ORDER BY that full path.






              share|improve this answer























                Your Answer








                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "182"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );






                Liliana del Carmen Castellanos is a new contributor. Be nice, and check out our Code of Conduct.









                draft saved

                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f234368%2forder-table-by-two-columns%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                5














                Another option:



                select a, b
                from t
                order by
                case when a < b or b is null then a else b end,
                case when a < b or b is null then b else a end ;


                Test in dbfiddle.uk.






                share|improve this answer























                • Can't you just order by isnull(id2, id1), id2?

                  – bendataclear
                  yesterday






                • 1





                  @bendtaclear yes. When I wrote the answer, it wasn't clear that first column can never be null.

                  – ypercubeᵀᴹ
                  yesterday











                • These really work for me :D

                  – Liliana del Carmen Castellanos
                  yesterday















                5














                Another option:



                select a, b
                from t
                order by
                case when a < b or b is null then a else b end,
                case when a < b or b is null then b else a end ;


                Test in dbfiddle.uk.






                share|improve this answer























                • Can't you just order by isnull(id2, id1), id2?

                  – bendataclear
                  yesterday






                • 1





                  @bendtaclear yes. When I wrote the answer, it wasn't clear that first column can never be null.

                  – ypercubeᵀᴹ
                  yesterday











                • These really work for me :D

                  – Liliana del Carmen Castellanos
                  yesterday













                5












                5








                5







                Another option:



                select a, b
                from t
                order by
                case when a < b or b is null then a else b end,
                case when a < b or b is null then b else a end ;


                Test in dbfiddle.uk.






                share|improve this answer













                Another option:



                select a, b
                from t
                order by
                case when a < b or b is null then a else b end,
                case when a < b or b is null then b else a end ;


                Test in dbfiddle.uk.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered yesterday









                ypercubeᵀᴹypercubeᵀᴹ

                78.5k11137221




                78.5k11137221












                • Can't you just order by isnull(id2, id1), id2?

                  – bendataclear
                  yesterday






                • 1





                  @bendtaclear yes. When I wrote the answer, it wasn't clear that first column can never be null.

                  – ypercubeᵀᴹ
                  yesterday











                • These really work for me :D

                  – Liliana del Carmen Castellanos
                  yesterday

















                • Can't you just order by isnull(id2, id1), id2?

                  – bendataclear
                  yesterday






                • 1





                  @bendtaclear yes. When I wrote the answer, it wasn't clear that first column can never be null.

                  – ypercubeᵀᴹ
                  yesterday











                • These really work for me :D

                  – Liliana del Carmen Castellanos
                  yesterday
















                Can't you just order by isnull(id2, id1), id2?

                – bendataclear
                yesterday





                Can't you just order by isnull(id2, id1), id2?

                – bendataclear
                yesterday




                1




                1





                @bendtaclear yes. When I wrote the answer, it wasn't clear that first column can never be null.

                – ypercubeᵀᴹ
                yesterday





                @bendtaclear yes. When I wrote the answer, it wasn't clear that first column can never be null.

                – ypercubeᵀᴹ
                yesterday













                These really work for me :D

                – Liliana del Carmen Castellanos
                yesterday





                These really work for me :D

                – Liliana del Carmen Castellanos
                yesterday













                8














                Your data:



                CREATE TABLE dbo.WEIRD_ORDERING
                (
                ID1 INT NULL,
                ID2 INT NULL
                );

                INSERT INTO dbo.WEIRD_ORDERING VALUES
                (242, NULL),
                (243, NULL),
                (244, NULL),
                (252, 242),
                (254, NULL),
                (255, NULL),
                (256, NULL),
                (292, NULL),
                (308, NULL),
                (311, NULL),
                (313, 311),
                (314, 311),
                (323, NULL),
                (324, 311),
                (335, 242),
                (340, NULL),
                (341, NULL),
                (358, NULL),
                (372, NULL),
                (373, NULL),
                (377, NULL),
                (378, 358),
                (379, 358),
                (380, 358),
                (381, 358);


                This gives you results in the desired order:



                SELECT ID1, ID2
                FROM
                (
                SELECT ID1, ID2, ID1 AS ORDERING_COLUMN
                FROM dbo.WEIRD_ORDERING
                WHERE ID1 <= ID2 OR ID2 IS NULL

                UNION ALL

                SELECT ID1, ID2, ID2 AS ORDERING_COLUMN
                FROM dbo.WEIRD_ORDERING
                WHERE ID1 > ID2 OR ID1 IS NULL
                ) q
                ORDER BY ORDERING_COLUMN;


                db fiddle






                share|improve this answer























                • Thank you so much. You are AWESOME!! :D

                  – Liliana del Carmen Castellanos
                  yesterday











                • This doesn't quite work, if you reorder your input data from (313, 311), (314, 311) to (314, 311),(313, 311) it comes out in the wrong order, I suggest updating the order by statement to be ORDER BY ORDERING_COLUMN, ID1 to fix that

                  – Xynos
                  yesterday











                • @Xynos feel free to edit

                  – Joe Obbish
                  yesterday















                8














                Your data:



                CREATE TABLE dbo.WEIRD_ORDERING
                (
                ID1 INT NULL,
                ID2 INT NULL
                );

                INSERT INTO dbo.WEIRD_ORDERING VALUES
                (242, NULL),
                (243, NULL),
                (244, NULL),
                (252, 242),
                (254, NULL),
                (255, NULL),
                (256, NULL),
                (292, NULL),
                (308, NULL),
                (311, NULL),
                (313, 311),
                (314, 311),
                (323, NULL),
                (324, 311),
                (335, 242),
                (340, NULL),
                (341, NULL),
                (358, NULL),
                (372, NULL),
                (373, NULL),
                (377, NULL),
                (378, 358),
                (379, 358),
                (380, 358),
                (381, 358);


                This gives you results in the desired order:



                SELECT ID1, ID2
                FROM
                (
                SELECT ID1, ID2, ID1 AS ORDERING_COLUMN
                FROM dbo.WEIRD_ORDERING
                WHERE ID1 <= ID2 OR ID2 IS NULL

                UNION ALL

                SELECT ID1, ID2, ID2 AS ORDERING_COLUMN
                FROM dbo.WEIRD_ORDERING
                WHERE ID1 > ID2 OR ID1 IS NULL
                ) q
                ORDER BY ORDERING_COLUMN;


                db fiddle






                share|improve this answer























                • Thank you so much. You are AWESOME!! :D

                  – Liliana del Carmen Castellanos
                  yesterday











                • This doesn't quite work, if you reorder your input data from (313, 311), (314, 311) to (314, 311),(313, 311) it comes out in the wrong order, I suggest updating the order by statement to be ORDER BY ORDERING_COLUMN, ID1 to fix that

                  – Xynos
                  yesterday











                • @Xynos feel free to edit

                  – Joe Obbish
                  yesterday













                8












                8








                8







                Your data:



                CREATE TABLE dbo.WEIRD_ORDERING
                (
                ID1 INT NULL,
                ID2 INT NULL
                );

                INSERT INTO dbo.WEIRD_ORDERING VALUES
                (242, NULL),
                (243, NULL),
                (244, NULL),
                (252, 242),
                (254, NULL),
                (255, NULL),
                (256, NULL),
                (292, NULL),
                (308, NULL),
                (311, NULL),
                (313, 311),
                (314, 311),
                (323, NULL),
                (324, 311),
                (335, 242),
                (340, NULL),
                (341, NULL),
                (358, NULL),
                (372, NULL),
                (373, NULL),
                (377, NULL),
                (378, 358),
                (379, 358),
                (380, 358),
                (381, 358);


                This gives you results in the desired order:



                SELECT ID1, ID2
                FROM
                (
                SELECT ID1, ID2, ID1 AS ORDERING_COLUMN
                FROM dbo.WEIRD_ORDERING
                WHERE ID1 <= ID2 OR ID2 IS NULL

                UNION ALL

                SELECT ID1, ID2, ID2 AS ORDERING_COLUMN
                FROM dbo.WEIRD_ORDERING
                WHERE ID1 > ID2 OR ID1 IS NULL
                ) q
                ORDER BY ORDERING_COLUMN;


                db fiddle






                share|improve this answer













                Your data:



                CREATE TABLE dbo.WEIRD_ORDERING
                (
                ID1 INT NULL,
                ID2 INT NULL
                );

                INSERT INTO dbo.WEIRD_ORDERING VALUES
                (242, NULL),
                (243, NULL),
                (244, NULL),
                (252, 242),
                (254, NULL),
                (255, NULL),
                (256, NULL),
                (292, NULL),
                (308, NULL),
                (311, NULL),
                (313, 311),
                (314, 311),
                (323, NULL),
                (324, 311),
                (335, 242),
                (340, NULL),
                (341, NULL),
                (358, NULL),
                (372, NULL),
                (373, NULL),
                (377, NULL),
                (378, 358),
                (379, 358),
                (380, 358),
                (381, 358);


                This gives you results in the desired order:



                SELECT ID1, ID2
                FROM
                (
                SELECT ID1, ID2, ID1 AS ORDERING_COLUMN
                FROM dbo.WEIRD_ORDERING
                WHERE ID1 <= ID2 OR ID2 IS NULL

                UNION ALL

                SELECT ID1, ID2, ID2 AS ORDERING_COLUMN
                FROM dbo.WEIRD_ORDERING
                WHERE ID1 > ID2 OR ID1 IS NULL
                ) q
                ORDER BY ORDERING_COLUMN;


                db fiddle







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 days ago









                Joe ObbishJoe Obbish

                21.9k43291




                21.9k43291












                • Thank you so much. You are AWESOME!! :D

                  – Liliana del Carmen Castellanos
                  yesterday











                • This doesn't quite work, if you reorder your input data from (313, 311), (314, 311) to (314, 311),(313, 311) it comes out in the wrong order, I suggest updating the order by statement to be ORDER BY ORDERING_COLUMN, ID1 to fix that

                  – Xynos
                  yesterday











                • @Xynos feel free to edit

                  – Joe Obbish
                  yesterday

















                • Thank you so much. You are AWESOME!! :D

                  – Liliana del Carmen Castellanos
                  yesterday











                • This doesn't quite work, if you reorder your input data from (313, 311), (314, 311) to (314, 311),(313, 311) it comes out in the wrong order, I suggest updating the order by statement to be ORDER BY ORDERING_COLUMN, ID1 to fix that

                  – Xynos
                  yesterday











                • @Xynos feel free to edit

                  – Joe Obbish
                  yesterday
















                Thank you so much. You are AWESOME!! :D

                – Liliana del Carmen Castellanos
                yesterday





                Thank you so much. You are AWESOME!! :D

                – Liliana del Carmen Castellanos
                yesterday













                This doesn't quite work, if you reorder your input data from (313, 311), (314, 311) to (314, 311),(313, 311) it comes out in the wrong order, I suggest updating the order by statement to be ORDER BY ORDERING_COLUMN, ID1 to fix that

                – Xynos
                yesterday





                This doesn't quite work, if you reorder your input data from (313, 311), (314, 311) to (314, 311),(313, 311) it comes out in the wrong order, I suggest updating the order by statement to be ORDER BY ORDERING_COLUMN, ID1 to fix that

                – Xynos
                yesterday













                @Xynos feel free to edit

                – Joe Obbish
                yesterday





                @Xynos feel free to edit

                – Joe Obbish
                yesterday











                3














                Stealing the sql fiddle table from Joe Obbish this is a simple solution



                SELECT * FROM dbo.WEIRD_ORDERING
                ORDER BY coalesce(ID2*1000+1, ID1*1000)





                share|improve this answer








                New contributor




                Spaceben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.
























                  3














                  Stealing the sql fiddle table from Joe Obbish this is a simple solution



                  SELECT * FROM dbo.WEIRD_ORDERING
                  ORDER BY coalesce(ID2*1000+1, ID1*1000)





                  share|improve this answer








                  New contributor




                  Spaceben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






















                    3












                    3








                    3







                    Stealing the sql fiddle table from Joe Obbish this is a simple solution



                    SELECT * FROM dbo.WEIRD_ORDERING
                    ORDER BY coalesce(ID2*1000+1, ID1*1000)





                    share|improve this answer








                    New contributor




                    Spaceben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.










                    Stealing the sql fiddle table from Joe Obbish this is a simple solution



                    SELECT * FROM dbo.WEIRD_ORDERING
                    ORDER BY coalesce(ID2*1000+1, ID1*1000)






                    share|improve this answer








                    New contributor




                    Spaceben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.









                    share|improve this answer



                    share|improve this answer






                    New contributor




                    Spaceben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.









                    answered yesterday









                    SpacebenSpaceben

                    311




                    311




                    New contributor




                    Spaceben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.





                    New contributor





                    Spaceben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.






                    Spaceben is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.





















                        2














                        SELECT *
                        FROM datatable
                        ORDER BY CASE WHEN field2 IS NULL THEN field1 ELSE field2 END, -- sort groups
                        CASE WHEN field2 IS NULL THEN 1 ELSE 2 END, -- move parent first
                        field2 -- sort childs





                        share|improve this answer



























                          2














                          SELECT *
                          FROM datatable
                          ORDER BY CASE WHEN field2 IS NULL THEN field1 ELSE field2 END, -- sort groups
                          CASE WHEN field2 IS NULL THEN 1 ELSE 2 END, -- move parent first
                          field2 -- sort childs





                          share|improve this answer

























                            2












                            2








                            2







                            SELECT *
                            FROM datatable
                            ORDER BY CASE WHEN field2 IS NULL THEN field1 ELSE field2 END, -- sort groups
                            CASE WHEN field2 IS NULL THEN 1 ELSE 2 END, -- move parent first
                            field2 -- sort childs





                            share|improve this answer













                            SELECT *
                            FROM datatable
                            ORDER BY CASE WHEN field2 IS NULL THEN field1 ELSE field2 END, -- sort groups
                            CASE WHEN field2 IS NULL THEN 1 ELSE 2 END, -- move parent first
                            field2 -- sort childs






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 2 days ago









                            AkinaAkina

                            4,9371311




                            4,9371311





















                                2














                                All answers so far rely on an assumption that there are no further levels of "generated from" (e.g. there is no 382 generated from the 381 which is itself generated from 352 - or which was it).



                                They should know what is said of "assume", so they should have asked you about this first.



                                If you can indeed have further levels of "generated from", then you need a recursive query to do the full path construction ("352" , "352/381" , "352/381/382" , etc etc) and ORDER BY that full path.






                                share|improve this answer



























                                  2














                                  All answers so far rely on an assumption that there are no further levels of "generated from" (e.g. there is no 382 generated from the 381 which is itself generated from 352 - or which was it).



                                  They should know what is said of "assume", so they should have asked you about this first.



                                  If you can indeed have further levels of "generated from", then you need a recursive query to do the full path construction ("352" , "352/381" , "352/381/382" , etc etc) and ORDER BY that full path.






                                  share|improve this answer

























                                    2












                                    2








                                    2







                                    All answers so far rely on an assumption that there are no further levels of "generated from" (e.g. there is no 382 generated from the 381 which is itself generated from 352 - or which was it).



                                    They should know what is said of "assume", so they should have asked you about this first.



                                    If you can indeed have further levels of "generated from", then you need a recursive query to do the full path construction ("352" , "352/381" , "352/381/382" , etc etc) and ORDER BY that full path.






                                    share|improve this answer













                                    All answers so far rely on an assumption that there are no further levels of "generated from" (e.g. there is no 382 generated from the 381 which is itself generated from 352 - or which was it).



                                    They should know what is said of "assume", so they should have asked you about this first.



                                    If you can indeed have further levels of "generated from", then you need a recursive query to do the full path construction ("352" , "352/381" , "352/381/382" , etc etc) and ORDER BY that full path.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered yesterday









                                    Erwin SmoutErwin Smout

                                    1,411712




                                    1,411712




















                                        Liliana del Carmen Castellanos is a new contributor. Be nice, and check out our Code of Conduct.









                                        draft saved

                                        draft discarded


















                                        Liliana del Carmen Castellanos is a new contributor. Be nice, and check out our Code of Conduct.












                                        Liliana del Carmen Castellanos is a new contributor. Be nice, and check out our Code of Conduct.











                                        Liliana del Carmen Castellanos is a new contributor. Be nice, and check out our Code of Conduct.














                                        Thanks for contributing an answer to Database Administrators Stack Exchange!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid


                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.

                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f234368%2forder-table-by-two-columns%23new-answer', 'question_page');

                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                                        Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

                                        Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?