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) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • 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