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) A socialist is someone who will give you the shirt off *someone else's* back.

  • 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
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    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