Sort Query by Data from another table

  • Hello:

    What is the best way to sort a query based on defined priorities in another table.

    I have my main 'Schedule' table here...

    2021-04-08 15_34_20-sage - Remote Desktop Connection

    And I want to sort the WorkCenterID column by the values in this table, 'Operations'.

    2021-04-08 15_36_03-sage - Remote Desktop Connection

    I am playing around a little with Joins, but I'm not even sure I'm on the right track.

    SELECT Schedule.JobID, Schedule.WorkCenterID, Schedule.ProcessID, Operations.Operation, Operations.OpOrder, Schedule.EstimatedProductionHours, Operations.OpOrder
    FROM Schedule INNER JOIN
    Operations ON Schedule.ProcessID = Operations.Operation
    ORDER BY Operations.OpOrder

     

    Thanks!

     

     

     

    Steve Anderson

  • That's correct.  You don't even need to include Operations.OpOrder in the SELECT results and you can still ORDER BY it.

    SQL DBA,SQL Server MVP(07, 08, 09) "Money can't buy you happiness." Maybe so, but it can make your unhappiness a LOT more comfortable!

  • I believe that your join should be

    FROM Schedule INNER JOIN
    Operations ON Schedule.WorkCenterID = Operations.Operation
  • ScottPletcher wrote:

    That's correct.  You don't even need to include Operations.OpOrder in the SELECT results and you can still ORDER BY it.

    Except when you use DISTINCT or GROUP BY - and some other query constructs, then - SQL requires the column in the order by to be in the select list.

    Jeffrey Williams
    Problems are opportunities brilliantly disguised as insurmountable obstacles.

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Thank you!

    Steve Anderson

  • Thank you.

    Steve Anderson

  • This was removed by the editor as SPAM

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply