sql - Getting detail with the highest priority using Joins/subqueries -
i hope can me problem: have 3 tables, similar this:
order order_id | order_date ===================== 1 | 01/01/2001 2 | 02/01/2001 3 | 03/01/2001 4 | 04/01/2001 5 | 05/01/2001 order_detail order_detail_id | order_id | status_id ====================================== 1 | 1 | 1 2 | 1 | 1 3 | 1 | 2 4 | 2 | 2 5 | 2 | 3 6 | 3 | 3 7 | 3 | 3 status status_id | status_name | status_priority ========================================= 1 | pending | 3 2 | completed | 2 3 | cancelled | 1
now, suppose shows, each row in order_detail table related order table using order_id, , has status indicated status_id. also, status table has status_priority column. need show each order, along with, among other columns, status highest priority among order details each order has, this:
order_id | order_date | status_name =================================== 1 | 01/01/2001 | pending 2 | 02/01/2001 | completed 3 | 03/01/2001 | cancelled 4 | 04/01/2001 | 5 | 05/01/2001 |
in case, example, since order_id 1 has @ least 1 order_detail_id pending status, has highest priority among details has, that's 1 appears. tried using join subquery, based on similar code have, can't seem adapt case. appreciated. in advance.
select columns, join tables order_id, order status_priority
Comments
Post a Comment