I have one main table (JOBS) which contains a price field (JOBS.PRICE). Some of the jobs in this table however can have adjusted bid prices on them, if the job is available for bids, these prices are then held in a separate table named (JOBBIDS). A single job may have multiple bids; I am only interested in sorting by the highest value in (JOBBIDS.CURRENT_BID). How would I go about sorting the job list by JOBS.PRICE and also (JOBBIDS.CURRENT_BID = MAX CURRENT_BID). Example:
First off, I hope that your PRICE column is not a string, meaning you should not be storing the dollar sign in there. That is going to make your work much more difficult. Of what column type have you defined PRICE?
You could write the query a number of ways, two off the top of my head are ... SELECT JOB, MAX(PRICE) AS PRICE FROM (SELECT ID, JOB, PRICE FROM JOBS UNION SELECT JOBBIDS.JOBID AS ID, JOBS.JOB AS JOB, CURRENT_BID AS PRICE FROM JOBBIDS INNER JOIN JOBS ON (JOBBIDS.JOBID = JOBS.ID)) AS groupedJobs GROUP BY JOB ORDER BY PRICE DESC; SELECT JOBS.JOB, MAX(groupedJobs.PRICE) AS PRICE FROM JOBS INNER JOIN (SELECT J.ID, J.PRICE FROM JOBS AS J UNION SELECT JOBBIDS.JOBID, CURRENT_BID FROM JOBBIDS) AS groupedJobs ON(JOBS.ID = groupedJobs.ID) GROUP BY JOBS.JOB ORDER BY PRICE DESC;
Either query produces the correct results but something about this just isn't right to me. It may be the relative table structure (design) or it may be the way I threw the queries together. Joining the data by ID is key but perhaps a logic structure such as CASE/IF to check for NULL 'OPENTOBIDS' may be something to look into. It should get you started in the right direction anyway. Maybe somebody else with a bit more time than I have right now can chime in ...